Speed up encryption/decryption?

后端 未结 5 1584
后悔当初
后悔当初 2020-12-09 14:20

I have an encryption and decryption code which I use to encrypt and decrypt video files (mp4). I\'m trying to speed up the decryption process as the encryption one is not th

相关标签:
5条回答
  • 2020-12-09 14:30

    bytebiscuit, your question gave me the solution which I am trying from past 6 days. I just modified your code little bit, and my 52 mb video file is getting decrypted in just 4 seconds. Previous decrypting technique took 45 seconds which was a different logic (not yours) . Thats a massive difference 45 seconds to 4 seconds. Where ever I have done modification I am putting //modified comment lines. I am sure if your video is 10mb video, it will get decrypted in 1 second for sure. Try applying this, it should work out.

    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);
    
    }
    
    0 讨论(0)
  • 2020-12-09 14:31

    I suggest you use the profiling tool provided in the android sdk. it will tell you where you spend the most time (i.e. : file writing or decoding).

    see http://developer.android.com/guide/developing/debugging/debugging-tracing.html

    This work on the emulator as well as on an actual device.

    0 讨论(0)
  • 2020-12-09 14:35

    Consider using the NDK. On devices before Froyo (and even Froyo itself), it would be really slow due to the lack of JIT (or a very simple one in Froyo). Even with the JIT, native architecture-optimized crypto code will always outrun Dalvik.

    See also this question.

    As an aside, if you're using AES directly, you're probably doing something wrong. If this is part of an effort to do DRM, make sure you realize the full extent of the fact that decompiling an Android app is trivial. Your key will not be secure, which by definition defeats the encryption.

    0 讨论(0)
  • 2020-12-09 14:37

    Why are you decrypting data only by blockSize increments ? You do not show what type of object cipher is, but I am guessing this is a javax.crypto.Cipher instance. It can handle update() calls over arrays of arbitrary length, and you will have much less overhead if you use longer arrays. You should process data by blocks of, say, 8192 bytes (that's the traditional length for a buffer, it interacts reasonably well with CPU inner caches).

    0 讨论(0)
  • 2020-12-09 14:54

    Instead of spending efforts to improve an inadequate architecture, you should consider a streaming solution: it has the great advantage to spread the computation time for the decryption so that it becomes no more noticeable. I mean: do not produce 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 make your own implementation or search for an existing one.

    0 讨论(0)
提交回复
热议问题