Understanding AES Modes of Operation
Compare ECB, CBC, CTR, GCM, and other AES modes to choose the right one for your use case.
Problem
You need to select an AES mode but are unsure how ECB, CBC, CTR, and GCM differ in security and performance.
Prerequisites
- Understanding of AES block cipher
- Knowledge of IVs and padding
Steps
-
1. Review the mode characteristics
ECB encrypts each block independently and leaks patterns. CBC chains blocks with an IV and requires padding. CTR turns AES into a stream cipher and needs no padding. GCM adds authentication on top of CTR. Each mode has different security properties and performance trade-offs.
Inputmodes: ECB, CBC, CTR, GCMOutputGCM recommended for most uses -
2. Evaluate authentication needs
If you need to detect tampering, use an authenticated mode like GCM or ChaCha20-Poly1305. CBC and CTR provide confidentiality only; an attacker can modify the ciphertext undetected. Adding an HMAC over the ciphertext is an alternative for non-GCM modes.
Inputneed integrity?Outputuse GCM or Encrypt-then-MAC -
3. Choose based on constraints
For most modern applications, GCM is the default choice: it provides authenticated encryption and is hardware-accelerated. Use CTR for streaming contexts where authentication is handled separately. Never use ECB for more than a single block.
Inputgeneral purposeOutputAES-256-GCM
Expected result
An informed choice of AES mode that provides the needed security properties for your use case.
Common errors
- Using ECB for multi-block data
- ECB leaks patterns. The famous ECB penguin image demonstrates how identical plaintext blocks produce identical ciphertext blocks. Use CBC or GCM instead.
- Using CBC without authenticating the ciphertext
- CBC is malleable. Apply an HMAC over the IV and ciphertext (encrypt-then-MAC) or switch to GCM.
Troubleshooting
- If performance is critical, GCM is hardware-accelerated on modern CPUs via AES-NI.
- If you need a stream cipher without padding, CTR mode is the right choice.
Security limits
- Always prefer authenticated encryption (GCM). Non-authenticated modes allow ciphertext manipulation.
Related tools
Related glossary
Related tasks
Ready to run it?