Speed ​​up encryption / decryption?

I have an encryption and decryption code that I use to encrypt and decrypt video files (mp4). I am trying to speed up the decryption process because encryption is not suitable for my business. This is the code I have for the decryption process:

private static void  decryptFile() throws IOException, ShortBufferException, IllegalBlockSizeException, BadPaddingException
    {

        //int blockSize = cipher.getBlockSize();
        int blockSize = cipher.getBlockSize();
        int outputSize = cipher.getOutputSize(blockSize);
        System.out.println("outputsize: " + outputSize);
        byte[] inBytes = new byte[blockSize];
        byte[] outBytes = new byte[outputSize];
        in= new FileInputStream(inputFile);
        out=new FileOutputStream(outputFile);

        BufferedInputStream inStream = new BufferedInputStream(in);
        int inLength = 0;;
        boolean more = true;
        while (more)
          {
             inLength = inStream.read(inBytes);
             if (inLength == blockSize)
             {
                int outLength 
                   = cipher.update(inBytes, 0, blockSize, outBytes);
                out.write(outBytes, 0, outLength);

             }
             else more = false;         
          }
          if (inLength > 0)
             outBytes = cipher.doFinal(inBytes, 0, inLength);
          else
             outBytes = cipher.doFinal();

          out.write(outBytes);

}

My question is how to speed up the decryption process in this code. I tried to decrypt a 10 MB mp4 file and it decrypts in 6-7 seconds. However, I aim for <1 second. Another thing I would like to know is that writing to FileOutputStream actually slows down the process, not the decryption process itself. Any suggestions on how to speed things up here.

I use AES for encryption / decryption.

, ProgressDialog, , (, : decrypted).

+3
5

blockSize ? , cipher , , javax.crypto.Cipher. update() , , . , , 8192 ( , ).

+4

bytebiscuit, , 6 . , 52- 4 . 45 , ( ). 45 4 . , // . , - 10 , 1 . , .

private static void  decryptFile() throws IOException, ShortBufferException, IllegalBlockSizeException, BadPaddingException
    {

        //int blockSize = cipher.getBlockSize();
        int blockSize = cipher.getBlockSize();
        int outputSize = cipher.getOutputSize(blockSize);
        System.out.println("outputsize: " + outputSize);
        byte[] inBytes = new byte[blockSize*1024]; //modified
        byte[] outBytes = new byte[outputSize * 1024]; //modified
        in= new FileInputStream(inputFile);
        out=new FileOutputStream(outputFile);

        BufferedInputStream inStream = new BufferedInputStream(in);
        int inLength = 0;;
        boolean more = true;
        while (more)
          {
             inLength = inStream.read(inBytes);
             if (inLength/1024 == blockSize) //modified
             {
                int outLength 
                   = cipher.update(inBytes, 0, blockSize*1024, outBytes);//modified
                out.write(outBytes, 0, outLength);

             }
             else more = false;         
          }
          if (inLength > 0)
             outBytes = cipher.doFinal(inBytes, 0, inLength);
          else
             outBytes = cipher.doFinal();

          out.write(outBytes);

}
+1

NDK. Froyo ( Froyo) - JIT ( Froyo). JIT, , , Dalvik.

. .

, AES , , , - .. DRM, , , Android . , .

-1

Instead of making efforts to improve an inadequate architecture, you should consider a streaming solution: it has the big advantage of extending the computation time for decryption to make it more visible. I mean: do not create another file from your video source, but rather a stream with a local http server. Unfortunately, there is no such component in the SDK, you have to execute your own implementation or search for an existing one.

-1
source

Source: https://habr.com/ru/post/1547771/


All Articles