Skip to main content
Be Bitwise

Modes of Operation: ECB, CBC, CTR

Symmetric Encryption · Networking & Cryptography track

9 min read

In this module

Beyond a Single Block

AES encrypts one 128-bit block at a time. Real data almost always overflows that 16-byte horizon. A mode of operation defines how the block cipher gets applied across an entire message. The mode choice carries serious weight — the wrong one leaks information even when the underlying cipher is mathematically airtight.

ECB Mode: The Dangerous Default

Electronic Codebook (ECB) is the simplest mode: each block is encrypted independently with the same key.

The problem: identical plaintext blocks produce identical ciphertext blocks. If blocks 1 and 3 hold the same bytes, their ciphertexts come out byte-for-byte identical too.

The ECB Penguin

The most famous demonstration of ECB's weakness is the encrypted penguin. Take a bitmap of Tux (the Linux mascot), chop it into blocks, encrypt each block with ECB. Because the image has wide regions of one colour, those regions stay identical in the ciphertext. The result is faintly absurd: Tux's silhouette is still clearly visible in the "encrypted" output.

The background area (all the same colour) encrypts to uniform blocks. The penguin's body (a different colour) encrypts to different uniform blocks. Outline preserved, secrecy zero.

Compare that to the same image encrypted under CBC, which looks like static — which is exactly what encrypted data should look like.

Key Concept

ECB mode encrypts blocks independently, preserving patterns in the data. The ECB penguin demonstrates this: encrypting an image with ECB reveals the outline because identical colour blocks encrypt to identical ciphertext blocks. Never use ECB for real encryption.

Here's the penguin encrypted for real with AES-128, once in ECB mode and once in CBC. Pick a new random key: the ECB colours change, the outline doesn't.

A 128×96 image, one byte per pixel, encrypted with AES-128. Each 16-byte block covers 16 pixels of one row. ECB encrypts equal blocks to equal ciphertext, so every flat patch of colour stays a recognisable pattern. CBC XORs each block with the previous ciphertext first, which hides the repeats.

Plaintext
AES-128-ECB
AES-128-CBC
Distinct 16-byte blocks in each image
ImageDistinct blocks
Plaintext112 of 768
ECB112 of 768
CBC768 of 768

Key 2b7e151628aed2a6abf7158809cf4f3c
IV 000102030405060708090a0b0c0d0e0f

CBC Mode: Chaining Blocks Together

Cipher Block Chaining (CBC) plugs ECB's pattern leak by chaining blocks together. Each plaintext block gets XORed with the previous ciphertext block before encryption.

The first block has no predecessor, so it uses an Initialisation Vector (IV): a random value that makes the encryption different every time, even for the same plaintext and key. It doesn't need to be secret, but it must be unpredictable: if an attacker can guess the next IV before choosing what gets encrypted, CBC leaks information (a later module shows how).

Because each block depends on the previous ciphertext, identical plaintext blocks now produce different ciphertext blocks. Pattern leak gone.

The drawback: CBC encryption is inherently sequential. Block 3 can't start until block 2 is finished. Decryption is another story: each block only needs the previous ciphertext block, which you already have, so it parallelises fine.

CTR Mode: Turning a Block Cipher into a Stream Cipher

Counter (CTR) mode tackles the problem from a totally different angle. Rather than encrypting the plaintext directly, it encrypts a sequence of counter values and XORs the results with the plaintext.

This has major advantages:

  • Parallelizable: Each block is independent. Encrypt all blocks simultaneously on a multi-core CPU.
  • Random access: Decrypt block 1000 without decrypting blocks 0-999.
  • No padding needed: XOR the last partial block with the corresponding keystream bytes.

Hard requirement: The nonce (number used once) must never be reused with the same key. Nonce reuse with CTR mode is catastrophic for the same reason as key reuse with XOR: an attacker XORs two ciphertexts and gets the XOR of the two plaintexts handed to them on a plate.

Time to see the three modes side by side. AES isn't available in the runner, so this program uses a toy 8-byte block cipher in its place (a few lines of Feistel network, utterly insecure). The message is "PAY BOB " twice, then "PAY ALL". Predict two things before you run it: which ECB blocks match, and what flipping one bit of ciphertext block 2 does to the decrypted text under CBC and under CTR.

Try itPredict the ECB repeats and the tampering damage, then try flipping a bit in a different block.
C (GCC 9.2.0)

ECB gives away the repeated "PAY BOB " instantly. Under CBC the flipped bit scrambles all of block 2 and flips exactly the same bit in block 3, which is how an attacker turns "PAY ALL" into "QAY ALL" without the key. CTR doesn't scramble anything: the bit flips in place, so the attacker gets a precise edit. Neither mode notices the tampering, which is the job GCM's tag does.

GCM Mode: Encryption with Authentication

Galois/Counter Mode (GCM) builds on CTR mode and bolts on authentication. It encrypts the data and produces an authentication tag that proves nobody has fiddled with the ciphertext in transit.

GCM is the gold standard for modern symmetric encryption because it delivers both confidentiality (the data stays secret) and integrity (you'll catch any tampering). TLS 1.3 leans on AES-GCM as its primary cipher suite. GCM also authenticates optional associated data (headers that travel in the clear but mustn't be altered), and it inherits CTR's hard rule: reuse a nonce under the same key and you leak the XOR of the plaintexts and hand the attacker enough to forge authentication tags.

Choosing a Mode

ModePattern-safeParallelAuthUse when
ECBNoYesNoNever use for encryption
CBCYesDecrypt onlyNoLegacy systems
CTRYesYesNoWhen you add auth separately
GCMYesYesYesDefault choice for new systems

The modern recommendation is unambiguous: use AES-GCM (or AES-GCM-SIV for nonce-misuse resistance) for symmetric encryption. Fast, parallelizable, authenticated, and well-supported in silicon.

Padding

Block ciphers demand input that's a multiple of the block size. Anything short of that needs padding. PKCS#7 fills the trailing bytes with the count of padding bytes required (need 5 bytes? pad with 05 05 05 05 05).

Padding looks innocuous, and yet it has bled an embarrassing number of CVEs. Padding oracle attacks exploit servers that behave subtly differently for valid versus invalid padding, dripping plaintext out one byte at a time. That's one of the bigger reasons CTR/GCM modes win in modern designs — no padding, no oracle.