【发布时间】:2017-05-23 10:04:29
【问题描述】:
您好,我正在创建一个示例来锁定 android 中的文件或文件夹。当我加密或解密大尺寸(超过 1 GB)文件时,我遇到了一个问题,它需要太多时间。 请帮我快速加密和解密文件。 这里我附上我在下面使用的代码
if (!isEncrypted) {
FileInputStream fis = new FileInputStream(path);
// This stream write the encrypted text. This stream will be wrapped by another stream.
FileOutputStream fos = new FileOutputStream(path + ".abcd");
// Length is 16 byte
SecretKeySpec sks = new SecretKeySpec("abcdefghijklmnop".getBytes(), "AES");
// Create cipher
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
// Wrap the output stream
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
// Write bytes
int b;
byte[] d = new byte[1024];
try {
while ((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
// Flush and close streams.
cos.flush();
cos.close();
fis.close();
new File(path).deleteOnExit();
isEncrypted = true;
runOnUiThread(new Runnable() {
@Override
public void run() {
btnEncrypt.setText("Decrypt Path");
deleteMyFile(path);
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
FileInputStream fis = new FileInputStream(path + ".abcd");
FileOutputStream fos = new FileOutputStream(path);
SecretKeySpec sks = new SecretKeySpec("abcdefghijklmnop".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[1024];
while ((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
isEncrypted = false;
runOnUiThread(new Runnable() {
@Override
public void run() {
deleteMyFile(path + ".abcd");
btnEncrypt.setText("Encrypt Path");
}
});
}
} catch (final Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setMessage(e.getMessage());
}
});
【问题讨论】:
-
“时间过长”有多长?此外,您使用的加密代码有许多弱点和不安全性。我不会在生产代码中使用它。
-
感谢 Luke Park 阅读我的问题。需要2分钟,我想在一秒钟内完成。是否可以在一秒钟内锁定大文件?请帮忙
-
"是否可以在一秒钟内锁定大文件?" - 那要看。您正在构建自己的硬件吗?如果是这样,欢迎您尝试使用高端组件构建它以尝试达到该速度。在普通的 Android 硬件上,你不能在一秒钟内写入 1GB,忽略任何加密/解密。在某些硬件上,如果您能在一分钟内写入 1 GB,我会印象深刻。
-
在我的 PC 上,我相信它有一个能够硬件加速 AES 的芯片,加密一个 ~1GB 文件需要 20.2 秒。普通手机(希望也不是高端手机!)远没有我的台式机那么快。这根本是不可能的。此外,您不是在“锁定”文件,而是在加密它。
-
有什么方法可以在不加密文件夹文件的情况下锁定文件夹?我想锁定文件夹,我不想加密文件,但我无法锁定文件夹。
标签: android file encryption