Lead with the decision: the mode is easier to get wrong than the cipher
AES defines how one 16-byte block is transformed; the mode defines how blocks are chained and what a failure exposes. Three lines:
- Default to AES-256-GCM: confidentiality and integrity in one pass, no padding to manage.
- Fall back to AES-256-CBC + HMAC only when your library lacks GCM, and implement encrypt-then-MAC yourself.
- Never use ECB on variable-length data — it cannot even hide identical plaintext blocks.
Each section below answers one question: under what conditions does this mode fail?
Blocks and padding: where most CBC pitfalls begin
AES processes 16 bytes at a time. A short final block gets PKCS7 padding: four missing bytes become \x04×4; a block that is already full gets an extra \x10×16 block so decryption can strip padding unambiguously.
plaintext: [12 bytes] → 4 bytes missing
padded: [12 bytes][\x04\x04\x04\x04] → 16 bytes
full block: [16 bytes] → append [\x10 × 16]
Whether the padding is valid becomes the single oracle signal in the next section.
ECB: identical plaintext blocks produce identical ciphertext blocks
block A ──AES──▶ ciphertext X
block B ──AES──▶ ciphertext Y
block A ──AES──▶ ciphertext X ← same A, same X
No chaining means the ciphertext preserves the plaintext's equality and position relationships. Without a key, an attacker can see that block 3 and block 11 hold the same data.
plaintext: [user=alice.....][card=4111...][user=alice.....]
ECB: [C1.............][C2..........][C1.............] ← structure leaks
Real data repeats through aligned fields, padding, flat image regions, and structured configs. What leaks is structure, not content — and structure is often sensitive: who belongs together, which records share a value. The only defensible use is encrypting one fixed-length, unpredictable block, such as wrapping a key with another key.
CBC: a random IV hides prefixes, not padding oracles
IV(random) C1 C2
│ │ │
block1 ─⊕─▶ AES ─▶ C1 block2 ─⊕─▶ AES ─▶ C2
Each block is XORed with the previous ciphertext block first. The first block has no predecessor, so a random IV fills that role: generate it fresh for every encryption and prepend it in the clear alongside the ciphertext. Reusing an IV is concrete: two plaintexts sharing a prefix under the same key produce ciphertexts that visibly share their opening.
A padding oracle needs three conditions at once:
- The server decrypts with CBC and checks padding before checking the MAC, or has no MAC at all;
- The attacker can submit arbitrary ciphertext repeatedly and observe two distinguishable responses — padding error versus another error — through a status code, an error message, a response length, or even timing;
- The attacker controls the IV or the preceding ciphertext block, since that determines the next block's decryption input.
With all three, the attacker recovers plaintext byte by byte from the last block backwards, without any key. The whole technique is one sentence: flip ciphertext bits and treat "padding valid/invalid" as a one-bit oracle.
Unifying error messages raises the bar but does not fix the flaw. Authenticate first, decrypt second (encrypt-then-MAC), or move to an AEAD. CBC carries two more inherent costs: encryption is serial, and one corrupted ciphertext block damages the next plaintext block.
GCM: nonce reuse is not degraded strength, it is total failure
GCM combines CTR stream encryption with GMAC authentication. The nonce derives counters that produce the keystream, while a polynomial MAC covers ciphertext and AAD:
nonce ─▶ counter(1) ─▶ keystream ⊕ block1 ─▶ C1 ─┐
nonce ─▶ counter(2) ─▶ keystream ⊕ block2 ─▶ C2 ─┼─▶ GMAC tag
AAD ──────┘
Output = nonce ‖ ciphertext ‖ tag. The tag covers ciphertext and AAD, so any modified bit fails verification. No padding, parallelizable, and the default in TLS 1.3 and mainstream libraries.
The price: a nonce must be unique under a given key. Reusing one breaks two properties at once:
- Confidentiality: the same nonce yields the same keystream, so
C1 ⊕ C2 = P1 ⊕ P2. Two ciphertexts hand the attacker the XOR of the plaintexts; JSON, logs, and English prose carry enough redundancy to recover in pieces. - Integrity: nonce reuse hands the attacker multiple plaintext/ciphertext pairs under one keystream, enough to recover GMAC's authentication key and forge a valid tag, slipping modified ciphertext past verification.
So a GCM nonce incident is not "slightly weaker"; confidentiality and integrity fail together. Operationally, either use a random 96-bit nonce with a guarantee against collisions, or a strictly increasing counter. Either way, store the nonce with the ciphertext and tag.
One boundary is routinely missed: GCM proves the ciphertext and AAD were not altered, not that the ciphertext belongs to this user or session. When you need context binding, put the user or session identifier in the AAD; otherwise the ciphertext can be replayed under a victim's account.
The three modes at a glance
| Property | ECB | CBC | GCM |
|---|---|---|---|
| Confidentiality | ✔ | ✔ | ✔ |
| Integrity / anti-tamper | ✘ | Needs a separate HMAC | ✔ built-in tag |
| Padding | PKCS7 | PKCS7 | None |
| Parallel encryption | ✔ | ✘ | ✔ |
| IV / nonce | Not needed | Random and unique | Random and unique |
| Failure mode | Leaks structure | Padding oracle | Nonce reuse collapses both |
Reproducible results
Three actions expose the differences: encrypt the same 16-byte repeated content under ECB and CBC, and ECB repeats blocks; flip any byte of a GCM ciphertext and the tag fails; fix the CBC IV to a constant and encrypt twice, and identical plaintext yields an identical ciphertext opening.
Then run the same plaintext through ECB and CBC with OpenSSL (key and IV fixed for reproducibility):
echo -n "hello world" | openssl enc -aes-128-ecb -K 000102030405060708090a0b0c0d0e0f | xxd -p
# → 68f4a8b1a2e9d0c37c9b2f4d5e6a7183
echo -n "hello world" | openssl enc -aes-128-cbc -K 000102030405060708090a0b0c0d0e0f -iv f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff | xxd -p
# → completely different output
Feed ECB a repeating 16-byte plaintext and the ciphertext repeats in the same places — reproducible proof of structural leakage. Run full encrypt/decrypt cycles in the AES encrypt/decrypt tool; nothing leaves your browser.