【发布时间】:2015-01-18 21:47:33
【问题描述】:
我正在开发一个 android 应用程序来保护图像和视频,如 Vaulty 和 Keep safe。我正在尝试使用 AES-128 加密/解密技术来存储图像和视频。我通过分别拍摄 3 个大小为 5.13、4.76 和 5.31 的样本图像进行了尝试。但加密耗时分别为25s、22s、27s,解密耗时分别为31s、30s、34s。我正在 HTC One X 上测试它。
这样的速度对于我的应用来说是不可行的,因为用户会快速滚动和查看图像而不会中断。您能否建议我如何提高性能(速度)或者我应该切换到其他算法?您能否向我推荐任何其他技术,通过这些技术我可以快速加密/解密图像和视频,而不会过多地损害安全性。
我试过Vaulty 和Keep safe,它们都很快。据说 Vaulty 使用的是 AES-256,但它在加密和查看图像方面仍然非常快速且响应迅速。使用 AES-256 的 vaulty 怎么可能这么快?
我使用的代码是:
static void encrypt(String filename) throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
// Here you read the cleartext.
File extStore = Environment.getExternalStorageDirectory();
startTime = System.currentTimeMillis();
Log.i("Encryption Started",extStore + "/5mbtest/"+filename);
FileInputStream fis = new FileInputStream(extStore + "/5mbtest/"+filename);
// This stream write the encrypted text. This stream will be wrapped by
// another stream.
FileOutputStream fos = new FileOutputStream(extStore + "/5mbtest/"+filename+".aes", false);
// Length is 16 byte
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
"AES");
// Create cipher
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, sks);
// Wrap the output stream
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
// Write bytes
int b;
byte[] d = new byte[8];
while ((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
// Flush and close streams.
cos.flush();
cos.close();
fis.close();
stopTime = System.currentTimeMillis();
Log.i("Encryption Ended",extStore + "/5mbtest/"+filename+".aes");
Log.i("Time Elapsed", ((stopTime - startTime)/1000.0)+"");
}
static void decrypt(String filename) throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
File extStore = Environment.getExternalStorageDirectory();
Log.i("Decryption Started",extStore + "/5mbtest/"+filename+".aes");
FileInputStream fis = new FileInputStream(extStore + "/5mbtest/"+filename+".aes");
FileOutputStream fos = new FileOutputStream(extStore + "/5mbtest/"+"decrypted"+filename,false);
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
"AES");
// Create cipher
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.DECRYPT_MODE, sks);
startTime = System.currentTimeMillis();
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[8];
while ((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
stopTime = System.currentTimeMillis();
Log.i("Decryption Ended",extStore + "/5mbtest/"+"decrypted"+filename);
Log.i("Time Elapsed", ((stopTime - startTime)/1000.0)+"");
fos.flush();
fos.close();
cis.close();
}
【问题讨论】:
标签: android encryption cryptography aes bouncycastle