I am working on Android applications where I need to save videos on an SD card that cannot be transferred, so I encrypt and decrypt as needed with a help Facebook Concealthat works great if the video size is smaller.
Whenever I try to make encryption and decryption for large video files no more 10MBin GenyMotion running 2.3.7, it will work with OutOfMemoryException, which means that I am running out of the heap of memory allocated for my application, which cannot be but should be prevented.
I tried:
- IO Apache Common Utils Package
- Various I / O utilities
Facebook Hiding: says that when decrypting
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 been fully read or throw
away the data if an exception occurs.
, Facebook Conceal:
:
public void startEncryption() {
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));
OutputStream outputStream;
outputStream = crypto.getCipherOutputStream(fileStream, entity);
outputStream.write(FileUtils.readFileToByteArray(mPlainFile));
File mRenameTo = new File(mPlainFile.getPath());
mPlainFile.delete();
mEncryptedFile.renameTo(mRenameTo);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (CryptoInitializationException e) {
e.printStackTrace();
} catch (KeyChainException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
:
public String startDecryption() {
try {
FileInputStream fileStream = new FileInputStream(mPlainFile);
InputStream inputStream;
inputStream = crypto.getCipherInputStream(fileStream, entity);
ByteArrayOutputStream out = new ByteArrayOutputStream();
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);
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();
}
inputStream.close();
outputStream.close();
return mDecryptedFile.getPath();
} catch (IOException e) {
e.printStackTrace();
} catch (CryptoInitializationException e) {
e.printStackTrace();
} catch (KeyChainException e) {
e.printStackTrace();
}
return null;
}
- , ?