gpt4 book ai didi

android - Cipher.dofinal() 出现内存不足错误

转载 作者:行者123 更新时间:2023-11-29 17:14:30 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我正在为其使用 Android 密码类解密一个大文件。

代码:

private byte[] decrypt_chunk(byte[] data, ByteString chunk_encryption_key) {
SecretKeySpec skeySpec = new SecretKeySpec(chunk_encryption_key.toByteArray(), 1, 16, "AES");
Cipher cipher;
byte[] decrypted = new byte[0];
try {
cipher = Cipher.getInstance("AES/CFB/NoPadding");

cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(getIV(0)));

decrypted = cipher.doFinal(data);

} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (ShortBufferException e) {
e.printStackTrace();
}

return decrypted;
}

解密大文件时出现“OutOfMemory”错误。

我有以下问题:

  1. 当前代码中的哪些更改可以修复OOM 错误

  2. Cipher.update() 可以帮助解决这个问题吗?如果是,如何实现?

谢谢。

最佳答案

使用 Cipher.update() 方法是一种可能的方法。

但是在您的情况下,我建议您使用 CipherOutputStream反而。我假设您有一个用于从服务器检索数据的 InputStream 和一个用于将数据保存到文件中的 FileOutputStream。

使用 CipherOutputStream 非常简单,只需在 FileOutputStream 周围“包装”即可:

FileOutputStream fout = ...
CipherOutputStream cout = new CipherOutputStream(fout, cipher);

现在继续使用 cout 而不是 fout,您写入的所有内容都会自动加密。

关于android - Cipher.dofinal() 出现内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39593428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com