Modes of Operation and Authenticated Encryption
See why ECB leaks patterns, how CBC and CTR modes use IVs and nonces, and why authenticated encryption like AES-GCM is today's default choice.
Welcome back!
Step 1 of 13Learn
The penguin problem
AES encrypts exactly 16 bytes at a time. Real messages are longer, so we need a rule for using the block cipher on many blocks. That rule is a mode of operation, and choosing badly can ruin a perfect cipher.
The obvious approach is ECB (Electronic Codebook): split the message into 16-byte blocks and encrypt each one separately with the same key. It sounds reasonable. It's a disaster.
Because AES is deterministic, identical plaintext blocks produce identical ciphertext blocks. A famous demonstration encrypts a bitmap image of the Linux penguin mascot in ECB mode. Large areas of the same color are made of identical blocks, so they encrypt to identical ciphertext blocks, and the encrypted image still clearly shows the penguin's outline. The colors are scrambled, but the picture leaks.
The same leak happens with structured data. In a database encrypted with ECB, two customers with the same 16-byte value in the same field produce the same ciphertext, telling an attacker which records match. An attacker can also cut and paste ciphertext blocks between messages, rearranging fields without ever knowing the key.
The lesson: a secure cipher must not encrypt the same plaintext the same way twice. Every good mode adds something that varies, an initialization vector or a nonce, so that repeated data looks different each time.
Step 2 of 13Compare
ECB vs CBC on a picture
| Aspect | ECB | CBC |
|---|---|---|
| Identical plaintext blocks | Identical ciphertext blocks | Different ciphertext blocks |
| Encrypted penguin image | Outline still visible | Looks like random noise |
| Needs an IV | No | Yes, random and unpredictable |
| Verdict | Never use | Legacy only, with a MAC |
Step 3 of 13Practice · True or false
In ECB mode, two identical 16-byte plaintext blocks encrypted with the same AES key produce identical ciphertext blocks.
Step 4 of 13Learn
CBC and CTR modes
CBC (Cipher Block Chaining) fixes the pattern problem by linking blocks together. Before encrypting each plaintext block, XOR it with the previous ciphertext block. The first block has no predecessor, so it's XORed with an initialization vector (IV), a random 16-byte value that is sent along with the ciphertext (it doesn't need to be secret, but it must be unpredictable). Now identical plaintext blocks encrypt differently depending on everything before them, and encrypting the same message twice with fresh IVs gives completely different ciphertexts.
CBC has drawbacks. Encryption can't be parallelized, because each block depends on the previous one. The message must be padded to a whole number of blocks, and sloppy handling of padding errors led to a family of real attacks called padding oracle attacks, in which a server that reveals invalid padding versus other error lets an attacker decrypt messages byte by byte.
CTR (Counter) mode turns a block cipher into a stream cipher. Encrypt a sequence of counter blocks, nonce plus 0, nonce plus 1, nonce plus 2, and so on, to produce a keystream, then XOR it with the plaintext. CTR needs no padding, can encrypt and decrypt blocks in parallel, and can jump straight to any position in the data. But like every stream cipher, it inherits the iron rule: never reuse a nonce with the same key, or the keystream repeats.
Neither CBC nor CTR, on its own, protects integrity. That's the next problem to solve.
Step 5 of 13Practice · Diagram
CTR mode turns a block cipher into a stream cipher
Which step is the reason flipping one ciphertext bit flips exactly one plaintext bit?
Tap the shape that answers the question, then press Check.
- Nonce + counter
Counter goes 0, 1, 2, ... for each block. The nonce makes it unique per message.
- AES with the key
Encrypting the counter values produces a keystream, block by block.
- XOR with plaintext
Blocks are independent, so they can be encrypted in parallel.
- Ciphertext
Step 6 of 13Practice · Match the pairs
Match each mode to its defining feature.
Tap an answer, then tap the place where it belongs (or drag it there).
- ECB
- CBC
- CTR
- GCM
Step 7 of 13Learn
Encryption doesn't stop tampering
Suppose a bank encrypts the message PAY 0100 TO BOB in CTR mode. Mallory can't read it. But she knows the format, and she knows that each ciphertext byte is just the plaintext byte XOR a keystream byte. If she flips particular bits of the ciphertext, the same bits flip in the decrypted plaintext. With a little arithmetic she can change 0100 to 9100 without ever learning the key. This property is called malleability, and every stream cipher and CTR-based scheme has it.
CBC is malleable too, in a slightly messier way: flipping a bit in one ciphertext block garbles that block but flips the matching bit in the next decrypted block.
The defense is a message authentication code (MAC): a short tag computed from the ciphertext and a secret key. The recipient recomputes the tag and rejects the message if it doesn't match. Without the key, Mallory can't produce a valid tag for her altered message.
Combining encryption and a MAC by hand is surprisingly error-prone. Should you MAC the plaintext or the ciphertext? Encrypt first or MAC first? Real protocols got it wrong for years. The safe answer, encrypt then MAC, is now built directly into modern algorithms, which is the topic of the next step.
Step 8 of 13Practice · What would you do?
What would you do?
A developer encrypts session cookies with AES in CTR mode using a secret server key. The cookie contains role=user. They reason that since attackers can't decrypt the cookie, they can't change it. What's the flaw?
Step 9 of 13Learn
Authenticated encryption: AES-GCM and ChaCha20-Poly1305
Authenticated encryption with associated data (AEAD) provides confidentiality and integrity in one operation, designed and analyzed as a single unit so developers can't combine the parts wrongly.
An AEAD algorithm takes a key, a unique nonce, the plaintext and optionally some associated data: information that must not be altered but needn't be secret, such as a packet header or a user ID. It outputs the ciphertext plus an authentication tag, usually 16 bytes. Decryption checks the tag first; if anything has been modified, whether the ciphertext, the associated data, the nonce or the tag, decryption fails and returns no plaintext at all.
The two dominant AEAD algorithms are:
- AES-GCM (Galois/Counter Mode): AES in counter mode plus a fast authentication function. It's the most common cipher choice in HTTPS and is accelerated by hardware.
- ChaCha20-Poly1305: the ChaCha20 stream cipher with the Poly1305 authenticator, preferred where AES hardware isn't available.
Both require a unique nonce per message under the same key. With AES-GCM, nonce reuse is especially severe: besides leaking plaintexts, it can let an attacker forge valid tags. Common practice is a 96-bit nonce from a counter, or random nonces with a limit on how many messages a single key encrypts.
The modern rule of thumb is simple: use an AEAD mode, and preferably a well-reviewed library that chooses nonces for you, such as libsodium or your platform's standard crypto library.
Step 10 of 13Diagram
What goes into and out of AEAD
Tap a shape with to see more.
- Key, nonce, plaintext
The usual encryption inputs. The nonce must be unique per key.
- Associated data
Headers that stay readable (like a record ID) but must not be altered.
- AES-GCM / ChaCha20-Poly1305
Encrypts the plaintext and computes a tag over the ciphertext and associated data.
- Ciphertext + tag
On decryption, a wrong tag means reject everything: no partial output.
Step 11 of 13Note
Step 12 of 13Practice · Select all that apply
Which statements about AES-GCM are true? Select all that apply.
Select all that apply.
Step 13 of 13Practice · Choose one
You need to encrypt records in a new application. Which choice is best?
Lesson complete!