加速加密/解密? [英] Speed up encryption/decryption?

查看:28
本文介绍了加速加密/解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个加密和解密代码,用于加密和解密视频文件 (mp4).我正在尝试加快解密过程,因为加密过程与我的情况无关.这是我用于解密过程的代码:

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 that relevant for my case. This is the code that 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);

}

我的问题是如何加快这段代码中的解密过程.我试过解密一个 10MB 的 mp4 文件,它在 6-7 秒内解密.但是,我的目标是 <1 秒.我想知道的另一件事是,如果我写入 FileOutputStream 输出实际上是在减慢进程而不是解密进程本身.关于如何在此处加快速度的任何建议.

My question is how to speed up the decryption process in this code. I've tried decrypting a 10MB mp4 file and it decrypts in 6-7 seconds. However, I'm aiming for < 1 seconds. Another thing I would like to know is if my writing to the FileOutputStream out is actually slowing the process down rather than the decryption process itself. Any suggestions on how to go about speeding things up here.

我使用 AES 进行加密/解密.

I'm using AES for encryption/decryption.

在找到解决方案之前,我将使用 ProgressDialog 告诉用户等待视频被解密(显然,我不会使用这个词:解密).

Until I find a solution, I will be using a ProgressDialog which tells the user to wait until the video has been decrypted (Obviously, I'm not going to use the word: decrypted).

推荐答案

为什么你只按 blockSize 增量解密数据?您没有显示 cipher 是什么类型的对象,但我猜这是一个 javax.crypto.Cipher 实例.它可以处理对任意长度数组的 update() 调用,如果使用更长的数组,开销就会少得多.您应该按块处理数据,比如 8192 字节(这是缓冲区的传统长度,它与 CPU 内部缓存的交互相当好).

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).

这篇关于加速加密/解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆