I can encrypt small files of any type (MP4, MP3, JPG, PDF, DOCX), but when they are large it gives me an error of the type:
"Throwing OutOfMemoryError" Failed to allocate to 150835256 byte allocation with 25165824 free bytes and 121MB until OOM, max allowed footprint 435080136, growth limit 536870912 ""
the file is a 500mb video but it gives an error.
@RequiresApi(api = Build.VERSION_CODES.O)
public void Encriptar_Archivo() throws Exception {
//File archivo = new File(Ruta);
//Path direccion = Paths.get(("/storage/emulated/0/pelicula.mp4"));
File file = new File("/storage/emulated/0/pelicula.mp4");
InputStream is = new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1999368];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] prueba = buffer.toByteArray();
byte encriptado[];
encriptado = Cifrar (prueba);
decrypt (encriptado); //desencriptas el archivo
System.out.println(encriptado);
}
public static byte [] Cifrar(byte[] prueba) throws
UnsupportedEncodingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException,
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
final MessageDigest md = MessageDigest.getInstance("md5");
final byte[] digestOfPassword = md.digest("OOAA2011".getBytes("utf-8"));
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result= cipher.doFinal(prueba);
return result;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static byte[] decrypt(byte[] message) throws Exception {
final MessageDigest md = MessageDigest.getInstance("md5");
final byte[] digestOfPassword = md.digest("OOAA2011".getBytes("utf-8"));
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher decipher = Cipher.getInstance("DESede/ECB/PKCS7Padding");
decipher.init(Cipher.DECRYPT_MODE, key);
final byte[] plainText = decipher.doFinal(message);
//Path path = Paths.get("/storage/emulated/0/Ejemplo.docx");
// Files.write(path, plainText);
Files.write(new File("/storage/emulated/0/Ejemplo2.mp4").toPath(), plainText);
return plainText;
}