I was looking for a way to quickly encrypt large files in python.
After some digging, I ran across this post on stackoverflow.com. The basic idea here is to use standard python libraries to take a plaintext file and make an AES encrypted copy in a manner that is compatible with OpenSSL tools.
The code provided in the post work like a charm when collecting memory, disk space is a big issue. Most systems these days are running at least 8GB. Making two full size copies of a memory dump on a host machine is not practical even under ideal conditions. Even if I compress first, we still need enough free space to cover of the original plus the compressed copy. Sometimes that is not available.
To help with this issue, I adapted the script to encrypt the file as it reads it.
Here is the important tweak that I made:
So we are reading chunk of the file, encrypting it, then overwriting the chunk with encrypted data until we get to the end of the file.
Does this really work you ask? Of course it does.
Don't know why this feels like a magic trick but TA DA!
This doesn't fix all of my issues with moving huge files around but this does encrypt it locally before it is sent over the network without needing double the space.
Big thanks to Thjis van Dien for putting this solution out there.