Java 如何在android中使用AES减少解密时间
我现在正在研究音频文件加密和解密。这对我来说很好,但是解密速度太慢了。如何缩短解密时间?应该需要我猜这是一个javax.crypto.Cipher实例。它可以处理任意长度数组上的更新调用,如果使用更长的数组,则需要减少开销 Try应该按8192字节的块处理数据,这是缓冲区的传统长度,它与CPU内部缓存的交互相当好Java 如何在android中使用AES减少解密时间,java,performance,encryption,Java,Performance,Encryption,我现在正在研究音频文件加密和解密。这对我来说很好,但是解密速度太慢了。如何缩短解密时间?应该需要我猜这是一个javax.crypto.Cipher实例。它可以处理任意长度数组上的更新调用,如果使用更长的数组,则需要减少开销 Try应该按8192字节的块处理数据,这是缓冲区的传统长度,它与CPU内部缓存的交互相当好 CipherInputStream cis = new CipherInputStream(fis, encipher); byte[] buffer = new byte[8192]
CipherInputStream cis = new CipherInputStream(fis, encipher);
byte[] buffer = new byte[8192]; // Or larger (but use powers of 2)
int bytesRead;
while ((bytesRead = cis.read(buffer)) != -1)
{
fos.write(buffer, 0, bytesRead);
}
fos.flush();
。。。。
不要忘记关闭finally块中的所有流。说它应该不超过6分钟是没有意义的。您应该指定一个速率,以字节/秒或兆字节/秒为单位,或以一定数量/时间为单位。解密的速度有多快?您应该能够以几MB/s的速度运行。您还需要发布您的代码。抱歉,解密应在<6秒内完成。@SriL在6秒内解密多少数据?你确定你的表演受到加密的限制,而不是IO的限制吗?我必须不间断地播放这首歌,我必须在6秒钟内开始播放这首歌。解密没有数据限制。我们可以一部分一部分地解密。但我不知道如何一部分一部分地解密。我可以有一个示例代码吗?谢谢@Frank。我正在从服务器下载音频文件,下载时必须用密钥加密,并且应该保存在我的sd中当我想要播放这首歌的时候,这首歌应该在6秒内解密并播放。请帮我举个例子。如果这是无法正常工作的代码,请将其编辑到问题中,然后将其删除。如果它有效,接受它。它有效,但解密需要很多时间,我需要减少解密时间。
public class EncryptDecrypt {
public Cipher encryptcipher, decryptCipher;
String TAG = "EncryptDecrypt";
// The default block size
public static int blockSize = 16;
// Buffer used to transport the bytes from one stream to another
byte[] buf = new byte[blockSize]; // input buffer
byte[] obuf = new byte[8192]; // output buffer
// The key
byte[] key = null;
byte[] IV = null;
public EncryptDecrypt(String passwd) {
key = passwd.getBytes();
key = "SECRETSECRET_1SE".getBytes();
// default IV value initialized with 0
IV = new byte[blockSize];
initCiphers();
}
public void initCiphers() {
try {
// 1. create the cipher using Bouncy Castle Provider
encryptcipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
// 2. create the key
SecretKey keyValue = new SecretKeySpec(key, "AES");
// 3. create the IV
AlgorithmParameterSpec IVspec = new IvParameterSpec(IV);
// 4. init the cipher
encryptcipher.init(Cipher.ENCRYPT_MODE, keyValue, IVspec);
encryptcipher.getOutputSize(8192);
// 1 create the cipher
decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
// 2. the key is already created
// 3. the IV is already created
// 4. init the cipher
decryptCipher.init(Cipher.DECRYPT_MODE, keyValue, IVspec);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public String encryptData(URL inputFileName, String outputFileName) {
File outFilename = new File(outputFileName);
try {
InputStream fis;
OutputStream fos;
fis = new BufferedInputStream(inputFileName.openStream(), 1000);
fos = new BufferedOutputStream(new FileOutputStream(outFilename));
Log.i(TAG, "Output path:" + outFilename);
int bufferLength = (inputFileName.toString().length() > 10000000
? 10000000
: 1000);
byte[] buffer = new byte[8192];
int noBytes = 0;
byte[] cipherBlock = new byte[encryptcipher
.getOutputSize(buffer.length)];
int cipherBytes;
long startTime = System.currentTimeMillis();
while ((noBytes = fis.read(buffer)) != -1) {
cipherBytes = encryptcipher.update(buffer, 0, noBytes,
cipherBlock);
fos.write(cipherBlock, 0, cipherBytes);
}
// always call doFinal
cipherBytes = encryptcipher.doFinal(cipherBlock, 0);
fos.write(cipherBlock, 0, cipherBytes);
System.out
.println("encryption "
+ " took "
+ ((System.currentTimeMillis() - startTime) / 1000.0)
+ "s");
// close the files
fos.close();
fis.close();
Log.i("encrypt", "done");
}
catch (Exception ex) {
ex.printStackTrace();
}
return outFilename.toString().trim();
}
public String decryptData(String inputFileName, String outputFileName) {
InputStream fis;
OutputStream fos;
try {
fis = new BufferedInputStream(new FileInputStream(inputFileName));
fos = new BufferedOutputStream(new FileOutputStream(outputFileName));
byte[] buffer = new byte[8192];
int noBytes = 0;
byte[] cipherBlock = new byte[decryptCipher
.getOutputSize(buffer.length)];
int cipherBytes;
long startTime = System.currentTimeMillis();
while ((noBytes = fis.read(buffer)) != -1) {
cipherBytes = decryptCipher.update(buffer, 0, noBytes,
cipherBlock);
Log.d("BufferRead", "" + buffer);
fos.write(cipherBlock, 0, cipherBytes);
}
// allways call doFinal
cipherBytes = decryptCipher.doFinal(cipherBlock, 0);
fos.write(cipherBlock, 0, cipherBytes);
System.out
.println("decryption "
+ " took "
+ ((System.currentTimeMillis() - startTime) / 1000.0)
+ "s");
// close the files
fos.close();
fis.close();
Log.i("decrypt", "done");
} catch (Exception ex) {
ex.printStackTrace();
}
return outputFileName.toString().trim();
}
}
This is my code