I am using this method for encrypting a video file:
public static void encryptToBinaryFile(String password, byte[] bytes, File file) throws EncrypterException {
try {
final byte[] rawKey = getRawKey(password.getBytes());
final FileOutputStream ostream = new FileOutputStream(file, false);
ostream.write(encrypt(rawKey, bytes));
ostream.flush();
ostream.close();
} catch (IOException e) {
throw new EncrypterException(e);
}
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws EncrypterException {
try {
final SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
final Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
return cipher.doFinal(clear);
} catch (Exception e) {
throw new EncrypterException(e);
}
}
But It gives an error Outofmemoryerror saying rejecting allocation of 301023321 element.
1.Is the method I am using correct for such big files?
2.If yes why I am getting this error?What is solution?
3.If no please suggest some good method for that?
CipherInputStreamandCipherOutputStreamwill help you do that easily. I wrote a sample program to encrypt and decrypt a video file. Modify it as per your choice…I am generating a new key using
generateKey(). You can use a byte array too, to generate your own key….