Using Java and AES to encrypt large files
I tested my code with files below this value (10MB, 100MB, 500MB), and the encryption worked normally However, I encountered a problem with files larger than 1GB
"Exception in thread" main "java.lang.outofmemoryerror: Java heap space"
I tried to use - xmx8g to increase available memory, but there were no dice Some of my codes are as follows
File selectedFile = new File("Z:\\dummy.txt"); Path path = Paths.get(selectedFile.getAbsolutePath()); byte[] toencrypt = Files.readAllBytes(path); byte[] ciphertext = aesCipherForEncryption.doFinal(toencrypt); FileOutputStream fos = new FileOutputStream(selectedFile.getAbsolutePath()); fos.write(ciphertext); fos.close();
As far as I know, its behavior is that it tries to read the whole file immediately, encrypt it, and store it in another byte array instead of buffering and streaming Can anyone help me with some code tips?
I'm a beginner in coding, so I really don't know much about it. Any help will be appreciated
Solution
Don't even try to read an entire large file into memory Encrypt the buffer once Just use the properly initialized cipheroutputstream contained around the fileoutputstream to execute the standard replication loop You can use it for all files without special circumstances Use a buffer of 8K or higher
Edit the "standard copy loop" in Java as follows:
byte[] buffer = new byte[8192]; int count; while ((count = in.read(buffer)) > 0) { out.write(buffer,count); }
In this case, out = new cipheroutputstream (New fileoutputstream (selectedfile), cipher)