如何用decipher.doFinal解密(file_encrypt)
问题描述:
我在做文件的加密和解密。最初我读取字节数组中的文件并将该字节数组传递给我的加密方法。高达50MB的大小,我可以加密文件没有任何问题。但是,如果我将我的文件大小增加到80 MB,它将在cipher.doFinal()中声明out of memoryException。 那么如何加密更大的文件没有任何问题?也是doFinal()方法有任何大小限制。请告诉我。 ,这是我的代码:如何用decipher.doFinal解密(file_encrypt)
公共字符串解密(字节[] file_encrypt)抛出异常{
String key22 = myKey;
byte[] b = key22.getBytes();
final SecretKey key = new SecretKeySpec(b, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher decipher = Cipher.getInstance("DESede/CBC/NoPadding");
decipher.init(Cipher.DECRYPT_MODE, key, iv);
final byte[] plainText = decipher.doFinal(file_encrypt);
try {
String dir = Environment.getExternalStorageDirectory() + File.separator + ".android";
String dir2 = Environment.getExternalStorageDirectory() + File.separator + ".android/.androidmain";
File folder = new File(dir); //folder name
File folder2 = new File(dir2); //folder name
if (!folder.exists())
folder.mkdirs();
if (!folder2.exists())
folder2.mkdirs();
File file = new File(Environment.getExternalStorageDirectory() + File.separator + ".android/.androidmain/file");
if (file.exists()) {
// Toast.makeText(contInst, "111", Toast.LENGTH_SHORT).show();
} else {
// Toast.makeText(contInst, "3333", Toast.LENGTH_SHORT).show();
file.createNewFile();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(plainText);
bos.flush();
bos.close();
videoplay.setSource(Uri.fromFile(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return "ok";
}
答
当加密或解密,无论是纯文本和加密文本应在存储器(RAM )至少一次。
但Java允许您使用有限的内存。如果你想从50MB到80MB,可能在你的清单的应用程序中使用android:largeHeap=true
将起作用。但是如果你想要更高的限制,你可能想要通过分割文件和逐块执行加密来执行加密。
是的,我知道必须块大块字节数组,并尝试它,但没有工作 – golbahar