Lesson 3 of 9 · 20m
ECB Detection: When Ciphertext Leaks the Picture
Electronic Codebook mode exposes repeated plaintext blocks; learn to detect it in tokens, files, databases, and protocols before someone else does.
ECB is the mode that encrypts each block independently. The famous penguin image is a good visual, but the practical failure is more mundane: repeated customer IDs, repeated JSON keys, repeated zero-filled database fields, and repeated protocol headers all produce repeated ciphertext. Structure survives encryption.
Why ECB fails even with perfect AES
C[i] = E(K, P[i])The same plaintext block under the same key always produces the same ciphertext block. There is no IV, no chaining, and no randomness. Encryption becomes a deterministic substitution cipher with a 16-byte alphabet.
If plaintext is:
user_id=1000 role=user user_id=1000then any aligned repetition creates equal ciphertext blocks. An attacker does not need to decrypt anything to learn equality relationships, infer formats, or replay chunks.
Detection is often trivial
In a lab, submit repeated input and inspect ciphertext:
from collections import Counter
blocks = [ct[i:i + 16] for i in range(0, len(ct), 16)]
counts = Counter(blocks)
repeated = [b.hex() for b, n in counts.items() if n > 1]
print({"blocks": len(blocks), "repeated": len(repeated)})In production review, look for APIs that encrypt attacker-controlled plaintext and return ciphertext: encrypted user IDs in URLs, encrypted search parameters, encrypted export filenames, and custom session blobs. If changing input byte 1 changes only one block while byte 32 changes only another, you are looking at ECB or an equally bad deterministic construction.
Cut-and-paste is the second half
ECB blocks are independent, so an attacker can splice ciphertext blocks:
forged = ct[0:16] + ct[32:48] + ct[16:32]If block two decrypts to a role, tenant, amount, or expiry, rearranging blocks rearranges those fields. I have seen this in database encryption schemes where every row was encrypted independently and operators assumed copying rows was an application-level concern.
The correct replacement
Use a randomized or nonce-based authenticated encryption mode. Identical plaintext encrypted twice should produce different ciphertext:
nonce = os.urandom(12)
stored = nonce + AESGCM(key).encrypt(nonce, plaintext, b"record-v1")If equality is genuinely required — for a database lookup, for example — do not improvise deterministic encryption. Use a keyed blind index such as HMAC-SHA-256 with a separate key and store the encrypted value under a randomized AEAD scheme.
Field notes
- ECB leaks even after imperfect compression or base64 encoding; hardware-backed AES-ECB is still ECB.
- Repeated ciphertext blocks are evidence, but absence of repeats in one sample is not proof of safety.
- Deterministic encryption is a special-purpose tool with leakage analysis, not a drop-in replacement for AEAD.
- When reviewing protocols, ask what two plaintexts being equal reveals. Equality is often enough.
Was this lesson helpful?