我已经完成了加密和解密在Android文件下载 但我想提高文件解密时的时间表现。 我的问题是当我下载任何文件,所以我已经在那里添加加密,但在这个阶段,我显示进度加载器,所以它看起来不错,但是当文件完全下载并尝试打开该文件时,这次它是解密该文件它花费了很多时间。 这看起来很糟糕,我如何减少解密时间请帮助我,如果一些人有类似的经历,请在这里与我分享的是我的代码Android文件解密和加密需要时间
加密代码
byte data[] = new byte[1024];
String seed = "password";
byte[] rawKey = getRawKey(seed.getBytes());
SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
output = new CipherOutputStream(output, cipher);
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100)/lenghtOfFile));
output.write(data, 0, count);
}
解密代码这里: -
String newPath = sdCardPath + "/" + dPdfName;
File f1 = new File(newPath);
if (!f1.exists())
try {
f1.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
InputStream fis = new FileInputStream(f);
OutputStream fos = new FileOutputStream(f1);
String seed = "password";
byte[] rawKey = getRawKey(seed.getBytes());
SecretKeySpec skeySpec = new SecretKeySpec(rawKey,
"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
fis = new CipherInputStream(fis, cipher);
int b;
byte[] data = new byte[4096];
while ((b = fis.read(data)) != -1) {
// fos.write(cipher.doFinal(data), 0, b);
fos.write(data, 0, b);
}
fos.flush();
fos.close();
fis.close();
} catch (Exception e) {
// TODO: handle exceptionpri
e.printStackTrace();
}
获取行主要方法: -
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
+0
我想这需要花费的时间... –
+0
[Android](http://stackoverflow.com/questions/7282930/android-slow-aes-decryption)[crypto](http://stackoverflow.com/questions/4663912/slow-aes-decryption-in-android)[很慢](http://stackoverflow.com/questions/6257945/aes-decryption-on-android-too-slow-to-be-usable-将-NDK待更快,其他的思路?rq = 1),[very](http://stackoverflow.com/questions/7713166/speed-up-encryption-decryption)[slow](http://stackoverflow.com/questions/10619770/android-aes-encryption -decryption)。 –
+0
我需要实现更好的性能 –