gpt4 book ai didi

java - 使用facebook Conceal 解密文件时出现OutOfMemoryException

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:03:02 26 4
gpt4 key购买 nike

我正在开发 Android 应用程序,我需要将视频保存在 SD 卡中,它不能传输,这就是为什么我在需要时使用 Facebook Conceal 进行加密和解密,它工作得很好如果视频尺寸较小。

每当我尝试在运行 2.3.7 的 GenyMotion 中对不超过 10MB 的大型视频文件进行加密和解密时,它会因 OutOfMemoryException 而崩溃这意味着分配给我的应用程序的堆内存已用完,无法处理但必须阻止。

尝试过:

  • Apache Common Utils IO 包
  • 各种 IO 工具

Facebook Conceal : 解密时说

 You must read the entire stream to completion.
The verification is done at the end of the stream.
Thus not reading till the end of the stream will cause
a security bug. For safety, you should not
use any of the data until it's been fully read or throw
away the data if an exception occurs.

我调用的加密和解密代码 Facebook Conceal :

加密:

public void startEncryption() {
// Creates a new Crypto object with default implementations of
// a key chain as well as native library.
// Check for whether the crypto functionality is available
// This might fail if android does not load libaries correctly.
if (!crypto.isAvailable()) {
return;
}
OutputStream fileStream;
try {
File mEncryptedFile = new File(mPlainFile.getPath().substring(0,
mPlainFile.getPath().length() - 4)
+ "_encrypted"
+ mPlainFile.getPath().substring(
mPlainFile.getPath().length() - 4,
mPlainFile.getPath().length()));

fileStream = new BufferedOutputStream(new FileOutputStream(
mEncryptedFile));

// Creates an output stream which encrypts the data as
// it is written to it and writes it out to the file.
OutputStream outputStream;
outputStream = crypto.getCipherOutputStream(fileStream, entity);
outputStream.write(FileUtils.readFileToByteArray(mPlainFile));
// fileStream.flush();
// fileStream.close();
// outputStream.flush();
// outputStream.close();
// outputStream.flush();
File mRenameTo = new File(mPlainFile.getPath());
mPlainFile.delete();
mEncryptedFile.renameTo(mRenameTo);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CryptoInitializationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyChainException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

解密:

public String startDecryption() {
// Get the file to which ciphertext has been written.
try {
FileInputStream fileStream = new FileInputStream(mPlainFile);

// Creates an input stream which decrypts the data as
// it is read from it.
InputStream inputStream;
inputStream = crypto.getCipherInputStream(fileStream, entity);

ByteArrayOutputStream out = new ByteArrayOutputStream();
// org.apache.commons.io.output.ByteArrayOutputStream out = new
// org.apache.commons.io.output.ByteArrayOutputStream(1024);
// Read into a byte array.
// int read;
// byte[] buffer = new byte[1024];
// // You must read the entire stream to completion.
// // The verification is done at the end of the stream.
// // Thus not reading till the end of the stream will cause
// // a security bug.
// int i = 0;
// while ((read = inputStream.read(buffer)) != -1) {
// out.write(buffer, 0, read);
// Log.i(TAG, "bytearrayoutputstream "+i++ + " "+read + " " +
// buffer.length + " "+out.size());
// }

mDecryptedFile = new File(mPlainFile.getPath().substring(0,
mPlainFile.getPath().length() - 4)
+ "_decrypted"
+ (mPlainFile.getPath().substring(mPlainFile.getPath()
.length() - 4, mPlainFile.getPath().length())));

OutputStream outputStream = new FileOutputStream(mDecryptedFile);
// IOUtils.copy(inputStream, outputStream);

try {
final byte[] buffer = new byte[1024];
int read;

while ((read = inputStream.read(buffer)) != -1)
outputStream.write(buffer, 0, read);

outputStream.flush();
} catch (Exception e) {

} finally {
outputStream.close();
}

// out.writeTo(outputStream);
// out.flush();
// out.close();

// fileStream.close();
inputStream.close();
// outputStream.flush();
outputStream.close();
return mDecryptedFile.getPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CryptoInitializationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyChainException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

是否有任何解决方案可以变通并加密和解密大型视频文件?

最佳答案

看起来您正在将整个文件读入一个字节数组。您应该做的是创建一个输入流并将其提供给加密引擎。

所以这样:

 outputStream.write(FileUtils.readFileToByteArray(mPlainFile));

应该看起来像你的解密循环。

BufferedInputStream  bis = new BufferedInputStream(mPlainFile);
InputStream inputStream = crypto.getCipherInputStream(
bis,
entity);
while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}

不确定这是否是您最终得到的结果。我从文档中提取了加密解码。我认为这证实了他们的 Intent 是说必须读取流直到结束。

关于java - 使用facebook Conceal 解密文件时出现OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30598785/

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