Decrypt a file in parts

So, I have these large files (6 GB +) that I need to decrypt on a 32-bit computer. The general procedure that I used earlier was to read the entire file in memory, then pass it to the decryption function, and then write everything back to the file. This does not work due to memory limitations. I tried to transfer the file in parts to the decryption function, but it seemed to mess up the boundaries of where I split the file before sending it to the decryption function.

I tried to split the file in parts relative to the size of the key, but that doesn't seem to matter. I tried a byte array of size 2048, as well as a byte-aray of size 294, thinking it might be a special border, but no luck. I can see parts of the file correctly decrypted, but parts that are common gibberish.

Is it impossible to decrypt the file in pieces, NOT POSSIBLE? If there is a way, then how?

Here is my decryption function / my attempt to decrypt in parts.

private Path outFile; private void decryptFile(FileInputStream fis, byte[] initVector, byte[] aesKey, long used) { //Assume used = 0 for this function. byte[] chunks = new byte[2048]; //If this number is greater than or equal to the size of the file then we are good. try { if (outFile.toFile().exists()) outFile.toFile().delete(); outFile.toFile().createNewFile(); FileOutputStream fos = new FileOutputStream(outFile.toFile()); OutputStreamWriter out = new OutputStreamWriter(fos); IvParameterSpec spec = new IvParameterSpec(Arrays.copyOfRange(initVector, 0, 16)); SecretKeySpec key = new SecretKeySpec(aesKey, "AES"); Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, key, spec); int x; while ((x = fis.read(chunks, 0, chunks.length)) != -1) { byte[] dec = cipher.doFinal(Arrays.copyOfRange(chunks, 0, x)); out.append(new String(dec)); } out.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); LOG.error(ExceptionUtils.getStackTrace(e)); } } 
+4
source share
1 answer

Consider using Update Cipher # (byte [], int, int, byte [], int) instead of doFinal() for multi-part operations. This will take care of partial boundaries for you.

The last part of the decrypted data can be obtained by calling the doFinal(byte[] output, int outputOffset) method doFinal(byte[] output, int outputOffset) .

+3
source

Source: https://habr.com/ru/post/1500868/


All Articles