Encrypt a large file with AES in chunks
Encrypt files too large for a single AES operation by processing them in chunks with a streaming mode, then decrypt to recover.
- 1
For large files (hundreds of MB or GB), loading everything into memory for a single AES call is impractical. Process the file in fixed-size chunks (e.g. 1 MiB), each with its own IV, and concatenate the ciphertext chunks.
- 2 AES Encryptor →
For each chunk: derive the key once (from passphrase), generate a fresh IV per chunk, encrypt, and output IV || ciphertext for that chunk. The key derivation is amortized — do it once.
- 3 AES Decryptor →
To decrypt: for each IV || ciphertext chunk, decrypt with the same key and IV, then concatenate the plaintext chunks in order.
- 4 Base64 Encoder →
If the encrypted output must be text-safe (for JSON or email), Base64-encode each chunk or the full ciphertext.
- 5
Use GCM or CTR mode for streaming — they do not require padding and support parallel encryption. Never use ECB. For authenticated streaming, use GCM per chunk or encrypt-then-MAC the whole file.
Frequently asked questions
- Why generate a new IV for each chunk?
- Reusing an IV with the same key in GCM or CTR mode is catastrophic — it reveals XOR of plaintexts. A fresh random IV per chunk (stored with the ciphertext) prevents this. In CBC, IV reuse leaks the XOR of the first blocks.
- How do I verify the decrypted file matches the original?
- Compute a SHA-256 hash of the original before encryption and of the decrypted result. If the hashes match, the round-trip is lossless. For tamper detection, store the hash alongside the ciphertext.
Workflows that solve this
Learn more
Want to understand why?