Block Cipher Modes
ECB, CBC, CTR, GCM — why mode choice matters
Skills taught: crypto.block-modes
Block Cipher Modes of Operation
AES is a 16-byte block cipher. To encrypt messages longer than 16 bytes, you need a mode of operation. The mode determines how blocks are chained and what security properties you get. Choosing the wrong mode is a common source of real-world vulnerabilities.
ECB — Electronic Code Book (Never Use)
ECB encrypts each 16-byte block independently with the same key. The result: identical plaintext blocks produce identical ciphertext blocks. This leaks patterns catastrophically.
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import os
key = os.urandom(32)
# ECB — illustrating WHY it's broken
# A 32-byte message with repeated 16-byte blocks
message = b"YELLOW SUBMARINE" * 2 # two identical blocks
cipher = Cipher(algorithms.AES(key), modes.ECB())
enc = cipher.encryptor()
ciphertext = enc.update(message) + enc.finalize()
# The two halves of ciphertext are IDENTICAL!
print(ciphertext[:16].hex())
print(ciphertext[16:].hex())
# Output: same line twice — the structure of the data leaks through
# This is why the 'ECB penguin' demo exists:
# encrypting a bitmap with ECB still shows the original image outline
Warning: Never use ECB mode in production. It is demonstrably broken. If you encrypt an image in ECB mode, you can still see the shapes (the famous 'ECB penguin'). No real algorithm should ever default to ECB.
CBC — Cipher Block Chaining
CBC fixes ECB by XORing each plaintext block with the previous ciphertext block before encrypting. The first block is XORed with an IV (Initialisation Vector). This makes each ciphertext block dependent on all previous blocks.
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding as sym_padding
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os
key = os.urandom(32)
message = b"Secret data that must stay private"
# --- CBC: encryption only (no authentication) ---
iv = os.urandom(16)
padder = sym_padding.PKCS7(128).padder()
padded_msg = padder.update(message) + padder.finalize()
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
enc = cipher.encryptor()
cbc_ciphertext = enc.update(padded_msg) + enc.finalize()
print("CBC ciphertext:", cbc_ciphertext.hex())
# CBC has no way to detect if ciphertext was modified!
# --- GCM: encryption + authentication (AEAD) ---
gcm_key = AESGCM.generate_key(256)
nonce = os.urandom(12)
aesgcm = AESGCM(gcm_key)
gcm_ciphertext = aesgcm.encrypt(nonce, message, None)
print("GCM ciphertext:", gcm_ciphertext.hex())
# Flip a byte in ciphertext — decryption will raise InvalidTag
CBC vs CTR vs GCM
- AES-CBC: Block mode with IV; sequential; needs padding
- AES-GCM: Counter mode + GHASH authentication (AEAD)
Mode Summary Table
Mode | Confidentiality | Authentication | Parallelisable | Use
-----|-----------------|----------------|----------------|----
ECB | BROKEN | No | Yes | NEVER
CBC | Yes | No | Decrypt only | Legacy
CTR | Yes | No | Yes | Streaming (add MAC separately)
CCM | Yes | Yes (AEAD) | No | IoT/constrained devices
GCM | Yes | Yes (AEAD) | Yes | RECOMMENDED for new code
SIV | Yes | Yes (AEAD) | Partial | When nonce uniqueness is hard
Tip: When in doubt: use AESGCM from the cryptography library with a random 12-byte nonce. It handles padding, authentication, and cipher chaining correctly in one call.
Frequently Asked Questions
What is a padding oracle attack?
An attack on AES-CBC where an attacker modifies ciphertext and observes whether the server returns a 'padding error'. By repeating this, the attacker can decrypt any ciphertext byte by byte without the key. The fix is to use authenticated encryption (GCM) so modified ciphertext is rejected before padding is checked.
What is the 'ECB penguin'?
A famous demonstration where a bitmap image of a Linux penguin (Tux) is encrypted with AES-ECB. The resulting ciphertext still shows the outline of the penguin because identical pixel blocks encrypt to identical ciphertext blocks.
Should I ever use AES-CTR?
Only if you separately add an authentication MAC (HMAC-SHA256) over the ciphertext. This is called 'Encrypt-then-MAC'. It is easier and safer to just use AES-GCM, which does both automatically.