问题描述
我有一个加密和解密代码,我用来加密和解密视频文件(mp4)。我正在加速解密过程,因为加密与我的情况并不相关。这是我解密过程中的代码: 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的写入实际上是减慢了进程,而不是解密过程本身。任何有关如何加快处理速度的建议。
我正在使用AES进行加密/解密。
直到我找到一个解决方案,我将使用一个ProgressDialog,告诉用户等待视频被解密(显然,我不会使用这个词:解密)。
为什么只能通过 blockSize
增量解密数据?你不会显示什么类型的对象 cipher
是,但我猜这是一个 javax.crypto.Cipher
实例。它可以通过任意长度的数组来处理 update()
调用,如果使用更长的数组,那么你的开销就会少得多。您应该按照8192个字节(这是缓冲区的传统长度)来处理数据,它与CPU内部高速缓存交互得很好。
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);
}
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.
I'm using AES for encryption/decryption.
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).
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).
这篇关于加快加密/解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!