Skip to content
CryptographyInternals

03 / 11 · Modern cryptography

Modes of operation

AES encrypts 16 bytes. Everything longer depends on how the blocks are chained.

Modes
ECB, CBC, CTR, GCM
Standards
NIST SP 800-38A, 800-38D
TLS 1.3
AES-GCM, ChaCha20-Poly1305

A block cipher like AES is a fixed function: 16 bytes in, 16 bytes out, for a given key. Real messages are longer, so a mode of operation decides how to cut them into blocks and how those blocks interact. The choice matters as much as the cipher itself.

Below, a small padlock picture is encrypted for real with each mode. The naive one keeps the shape visible; the others turn it into noise, and the modern one also detects any change to the ciphertext.

Bytes
one pixel · (5, 16)
156red
156green
156blue
255alpha
1281
640
320
161
81
41
20
10

128 + 16 + 8 + 4 = 156

9 → 9

12 → c

156 =9c

a = 10 · b = 11 · c = 12 · d = 13 · e = 14 · f = 15

01 / 16 · Modes of operation

A picture is just numbers

Zoom on one grey pixel: it is stored as 4 numbers from 0 to 255 (red, green, blue, alpha). Each number is a byte, 8 bits: switches worth 128, 64, 32… down to 1, added up where they are on. Cut into two groups of 4, a byte is written as two hex characters (0 to 9, then a to f for 10 to 15): 1001 1100 is 9c.

Encryption never sees a picture, only bytes: everything that follows moves and flips these bits.

The basics: Bits and bytes →

How it works

  1. 1ECB encrypts each block on its own with the same key. Identical plaintext blocks give identical ciphertext blocks.
  2. 2CBC XORs each plaintext block with the previous ciphertext block before encrypting it; the first block uses a random initialization vector (IV).
  3. 3CTR encrypts a nonce followed by a counter (0, 1, 2…) to produce a keystream, then XORs it with the data. Blocks are independent, so the work runs in parallel and no padding is needed.
  4. 4GCM is CTR plus authentication: GHASH, a polynomial hash keyed by AES_K(0), runs over the ciphertext to produce a 128-bit tag. The receiver recomputes it and refuses to decrypt if it differs.

Why it holds

Hiding repeats
With a fresh IV or nonce, the same message encrypted twice gives two unrelated ciphertexts. An eavesdropper cannot even tell that two messages are equal.
Authenticated encryption
AEAD modes such as GCM and ChaCha20-Poly1305 protect integrity as well as confidentiality. A modified ciphertext is rejected before any plaintext is released, which shuts down whole families of active attacks.
Speed
CTR and GCM process every block independently, so they run in parallel and use the AES and carry-less multiplication instructions of modern CPUs. TLS 1.3 only allows AEAD modes.

What can go wrong

Nonce reuse
With CTR or GCM, encrypting two messages under the same key and nonce reuses the keystream: XORing the ciphertexts gives the XOR of the plaintexts. In GCM it also leaks the authentication key, allowing forgeries.
Unauthenticated modes
CBC and CTR alone let an attacker modify the ciphertext undetected. CBC with padding has fallen to padding oracle attacks (Vaudenay, 2002; POODLE against SSL 3.0, 2014).