5. Block Cipher Modes

How to safely encrypt more than one block

AES encrypts exactly 16 bytes at a time. Most messages are longer. A mode of operation defines how to apply a block cipher to an arbitrarily long message. The choice of mode is critical — the wrong mode can make a perfectly good cipher completely insecure.

ECB — Electronic Codebook (The Wrong Way)

The simplest mode: split the message into 16-byte blocks and encrypt each one independently.

ECB mode

P₁ ─→ [AES_K] ─→ C₁
P₂ ─→ [AES_K] ─→ C₂
P₃ ─→ [AES_K] ─→ C₃
...

Same plaintext block → Same ciphertext block. Always.

ECB Is Insecure

Because identical plaintext blocks produce identical ciphertext blocks, patterns in the plaintext are visible in the ciphertext. The classic demonstration: encrypt a bitmap image with ECB, and the outline of the image is clearly visible in the ciphertext — the "ECB penguin."

The ECB penguin problem

Original image:        ECB encrypted:         CBC encrypted:

████████████ ████████████ ░▒▓█▒░▓▒█▓░█ ██ ████ ██ ██ ████ ██ █▓░▒▓▒░█▒▓░▒ ██ ████ ██ ██ ████ ██ ▒░▓█▒▓░▒▓█▒░ ██████████████ ██████████████ ▓▒░█▓▒░▓█▒░▓ ████ ██ ████ ████ ██ ████ ░▓▒█░▓▒░█▓▒░ ████████████ ████████████ █▒░▓▒█░▓█▒▓▒

Pattern preserved! Pattern visible! Looks random ✓

Never use ECB for encrypting more than one block. It exists mainly as a building block and a cautionary tale.

CBC — Cipher Block Chaining

CBC fixes ECB's biggest problem: before encrypting each block, XOR it with the previous ciphertext block. This means identical plaintext blocks produce different ciphertext.

CBC encryption

        IV (random)
        │
P₁ ──→ ⊕ ──→ [AES_K] ──→ C₁
                            │
P₂ ──→ ⊕ ──→ [AES_K] ──→ C₂
                            │
P₃ ──→ ⊕ ──→ [AES_K] ──→ C₃

CBC decryption

C₁ ──→ [AES⁻¹_K] ──→ ⊕ ←── IV  ──→ P₁
C₂ ──→ [AES⁻¹_K] ──→ ⊕ ←── C₁ ──→ P₂
C₃ ──→ [AES⁻¹_K] ──→ ⊕ ←── C₂ ──→ P₃

The Initialization Vector (IV) is a random 16-byte value sent alongside the ciphertext. It ensures that encrypting the same message twice produces different ciphertext each time.

Strengths

Identical blocks → different ciphertext. Well understood. Widely deployed.

Weaknesses

Sequential — can't parallelize encryption. Vulnerable to padding oracle attacks if not used with authentication.

CTR — Counter Mode

Counter mode turns a block cipher into a stream cipher. Instead of encrypting the plaintext directly, it encrypts a counter and XORs the result with the plaintext.

CTR mode

Nonce‖0 ──→ [AES_K] ──→ keystream₁ ──⊕── P₁ ──→ C₁
Nonce‖1 ──→ [AES_K] ──→ keystream₂ ──⊕── P₂ ──→ C₂
Nonce‖2 ──→ [AES_K] ──→ keystream₃ ──⊕── P₃ ──→ C₃

The block cipher encrypts the counter, not the plaintext.

Strengths

Fully parallelizable. Random access — decrypt any block without decrypting previous ones. No padding needed.

Weaknesses

Nonce must never repeat with the same key — reuse is catastrophic (same as OTP key reuse). No built-in authentication.

The Authentication Problem

Encryption ≠ Authentication

None of the modes above prevent an attacker from modifying the ciphertext. CBC and CTR guarantee confidentiality (can't read the data) but not integrity (can't tell if it's been tampered with). An attacker can flip bits in CTR ciphertext and predictably flip the corresponding plaintext bits.

Bit-flipping attack on CTR mode

Original:   C = P ⊕ keystream
Tampered:   C' = C ⊕ Δ = (P ⊕ keystream) ⊕ Δ

Decryption: P' = C' ⊕ keystream = P ⊕ Δ

The attacker flipped exactly the bits they wanted in the plaintext, without knowing the key or the plaintext!

This is why modern protocols always use authenticated encryption — modes that provide both confidentiality and integrity.

GCM — Galois/Counter Mode

GCM combines CTR-mode encryption with a polynomial hash (GHASH) to provide authenticated encryption with associated data (AEAD). It's the most widely used mode in modern protocols.

GCM combines encryption and authentication

              ┌─────────────────────────────────┐
              │         CTR encryption           │
Nonce‖ctr ─→ [AES_K] ─→ ⊕ ← plaintext ─→ ciphertext
              │                                  │
              │         GHASH authentication      │
              │  AAD + ciphertext + lengths ─→ GHASH ─→ Tag
              └─────────────────────────────────┘

Output: (ciphertext, tag) The tag authenticates both the ciphertext AND any unencrypted associated data (AAD), like packet headers.

  1. Encrypt with CTR mode

    Generate keystream blocks by encrypting incrementing counters, XOR with plaintext.

  2. Compute authentication tag

    Feed the AAD, ciphertext, and their lengths through GHASH (a polynomial MAC over GF(2128)).

  3. Encrypt the tag

    XOR the GHASH output with an encrypted nonce to produce the final authentication tag.

  4. On decryption: verify tag first

    Recompute the tag. If it doesn't match, reject the entire message — don't even try to decrypt.

Always Authenticate

In modern cryptography, unauthenticated encryption is considered a bug. If you're choosing a mode, choose GCM (or ChaCha20-Poly1305). Both provide AEAD and are supported by every modern TLS implementation.

Mode Comparison

Mode Parallel Auth IV/Nonce Use case
ECB Yes No None Never for multi-block data
CBC Decrypt only No Random IV Legacy systems
CTR Yes No Unique nonce Building block for GCM
GCM Yes Yes Unique nonce TLS, SSH, IPsec — the default choice

Padding

CBC requires the plaintext to be a multiple of the block size. If it's not, you need padding. The standard scheme is PKCS#7:

PKCS#7 padding (16-byte blocks)

Message: "HELLO" (5 bytes) — needs 11 bytes of padding

Padded: 48 45 4C 4C 4F 0B 0B 0B 0B 0B 0B 0B 0B 0B 0B 0B H E L L O ←── 11 bytes, each with value 0x0B (11) ──→

If the message is already a multiple of 16, add a full block of padding: 16 bytes each with value 0x10 (16)

CTR and GCM don't need padding — they operate as stream ciphers.

Real-World Usage

Protocol Mode
TLS 1.3 AES-128-GCM, AES-256-GCM, ChaCha20-Poly1305
TLS 1.2 AES-CBC (legacy), AES-GCM (preferred)
IPsec AES-GCM for ESP
SSH AES-256-GCM or AES-256-CTR with HMAC
BitLocker AES-XTS (specialized mode for disk encryption)
WPA3 AES-CCMP (CTR + CBC-MAC)

Key Takeaways

  • ECB encrypts blocks independently — identical blocks leak patterns. Never use it
  • CBC chains blocks together with XOR — fixes ECB but is sequential and needs padding
  • CTR turns a block cipher into a parallelizable stream cipher — but has no authentication
  • GCM = CTR + GHASH — provides both encryption and authentication (AEAD)
  • Encryption without authentication is dangerous — attackers can tamper with ciphertext undetected
  • Modern best practice: always use an AEAD mode (GCM or ChaCha20-Poly1305)

Try It Yourself — ECB vs CBC Visualization

This demonstrates why ECB mode is insecure: identical plaintext blocks produce identical ciphertext blocks in ECB, but not in CBC. Type repeating text to see the pattern problem in action.

ECB Mode

CBC Mode

Notice the Pattern

In ECB mode, identical plaintext blocks produce identical ciphertext blocks (same color). An attacker can see patterns in your data without decrypting it. CBC mode eliminates this by chaining blocks together — every ciphertext block depends on all previous blocks, so even identical plaintext blocks produce completely different ciphertext.


All the ciphers we've seen so far are symmetric — the same key encrypts and decrypts. But how do two parties who've never met agree on a shared key? That's the fundamental problem that public-key cryptography solves, starting with RSA.