CodingLad
cryptography

Block Cipher Modes of Operation Explained (ECB, CBC, CTR)

Block Cipher Modes of Operation Explained (ECB, CBC, CTR)
0 views
9 min read
#cryptography

Block Cipher Modes of Operation Explained (ECB, CBC, CTR)

Block ciphers like AES encrypt data in fixed-size blocks (128 bits for AES). However, real-world data is much larger than a single block. To securely encrypt long messages, block ciphers use modes of operation.

This blog explains ECB, CBC, and CTR modes, how they work, and why modern systems prefer CTR (or authenticated variants).

Why Do We Need Modes of Operation?

A block cipher alone can only encrypt one block at a time. Modes of operation define:

  • How blocks are linked together
  • How randomness is introduced
  • Whether encryption can be parallelized
  • Whether patterns in plaintext are hidden

Without a proper mode, encryption can leak information.

Key Insight:

  • Block ciphers operate on fixed-size blocks (e.g., 128 bits)
  • Real data is variable-length and often much larger
  • Modes of operation bridge this gap securely

1. ECB Mode (Electronic Code Book)

Idea

Each plaintext block is encrypted independently using the same key.

Formula

Encryption: Cj=EK(Pj)C_j = E_K(P_j)

Decryption: Pj=DK(Cj)P_j = D_K(C_j)

Where:

  • CjC_j = j-th ciphertext block
  • PjP_j = j-th plaintext block
  • EKE_K = encryption function with key KK

Key Characteristics

  • No IV or nonce — Each block encrypted independently
  • Same plaintext block → same ciphertext block
  • Deterministic — Identical inputs produce identical outputs

Advantages

  • Simple to implement — No chaining logic needed
  • Fully parallelizable — All blocks can be encrypted simultaneously
  • Low overhead — Minimal computational cost

Disadvantages (Critical)

  • ⚠️ Leaks patterns — Identical plaintext blocks produce identical ciphertext
  • ⚠️ No randomness — Same message always produces same ciphertext
  • ⚠️ Not secure for structured data — Images, files, databases reveal structure
  • ⚠️ Vulnerable to frequency analysis — Patterns in plaintext are visible

Why ECB Is Insecure

The Problem:

If plaintext has repeating patterns, the ciphertext will reveal those patterns directly.

Example:

  • Plaintext: HELLO HELLO HELLO
  • ECB encryption: Each "HELLO" block encrypts to the same ciphertext
  • Attacker can see: "This pattern repeats 3 times"

Visual Example:

  • Encrypting an image with ECB reveals the image structure
  • Encrypting a database with ECB reveals repeated values
  • Encrypting text with ECB reveals word boundaries

📌 ECB provides encryption but not security.

Real-World Impact:

  • ECB was used in early systems (now deprecated)
  • Still sometimes used incorrectly in educational examples
  • Never use ECB in production systems

2. CBC Mode (Cipher Block Chaining)

Idea

Each plaintext block is XORed with the previous ciphertext block before encryption.

This creates a chain where each block depends on all previous blocks.

Encryption

Cj=EK(PjCj1)C_j = E_K(P_j \oplus C_{j-1})

Where:

  • CjC_j = j-th ciphertext block
  • PjP_j = j-th plaintext block
  • Cj1C_{j-1} = previous ciphertext block
  • EKE_K = encryption function with key KK

Process:

  1. XOR plaintext block with previous ciphertext block
  2. Encrypt the result
  3. Use this ciphertext for the next block's XOR

Decryption

Pj=DK(Cj)Cj1P_j = D_K(C_j) \oplus C_{j-1}

Where:

  • DKD_K = decryption function with key KK

Process:

  1. Decrypt ciphertext block
  2. XOR with previous ciphertext block
  3. Recover original plaintext

Initialization Vector (IV)

The First Block Problem:

The first block has no previous ciphertext to XOR with.

Solution: IV (Initialization Vector)

  • First block uses an IV instead of C0C_0
  • IV is a random value (same size as a block)
  • IV ensures uniqueness even if plaintext repeats

Encryption with IV:

C0=EK(P0IV)C_0 = E_K(P_0 \oplus IV)

Cj=EK(PjCj1) for j>0C_j = E_K(P_j \oplus C_{j-1}) \text{ for } j > 0

Decryption with IV:

P0=DK(C0)IVP_0 = D_K(C_0) \oplus IV

Pj=DK(Cj)Cj1 for j>0P_j = D_K(C_j) \oplus C_{j-1} \text{ for } j > 0

IV Requirements:

  • Must be random or unpredictable
  • Must be unique for each encryption with the same key
  • Can be public (doesn't need to be secret)
  • Must be transmitted with the ciphertext

Advantages

  • Hides plaintext patterns — Identical blocks produce different ciphertext
  • More secure than ECB — Chaining prevents pattern leakage
  • Widely used historically — TLS 1.0, 1.1, early SSL
  • Deterministic decryption — Same key + IV + ciphertext = same plaintext

Disadvantages

  • ⚠️ Not parallelizable for encryption — Each block depends on previous
  • ⚠️ Error propagation — One bit error affects two blocks
  • ⚠️ Requires secure IV handling — IV must be unique and random
  • ⚠️ Padding required — Last block must be padded to block size
  • ⚠️ Slower than CTR — Sequential processing limits speed

Security Note

CBC prevents pattern leakage, but does not provide authentication.

What This Means:

  • CBC ensures confidentiality (hides plaintext)
  • CBC does not ensure integrity (cannot detect tampering)
  • Attacker can modify ciphertext without detection
  • Solution: Use authenticated encryption

IV Reuse Attack:

  • If same IV is reused with same key, security breaks
  • First blocks become predictable
  • Always use unique IVs

3. CTR Mode (Counter Mode)

Idea

CTR converts a block cipher into a stream cipher using a counter.

Instead of encrypting plaintext directly, it encrypts a counter value to generate a keystream, which is XORed with plaintext.

Encryption

Cj=PjEK(CTRj)C_j = P_j \oplus E_K(CTR_j)

Where:

  • CjC_j = j-th ciphertext block
  • PjP_j = j-th plaintext block
  • CTRjCTR_j = j-th counter value
  • EKE_K = encryption function with key KK

Process:

  1. Generate counter value CTRjCTR_j
  2. Encrypt counter: EK(CTRj)E_K(CTR_j) (produces keystream)
  3. XOR plaintext with keystream: PjEK(CTRj)P_j \oplus E_K(CTR_j)

Decryption

Pj=CjEK(CTRj)P_j = C_j \oplus E_K(CTR_j)

(The same operation works both ways.)

Why Decryption = Encryption:

  • XOR is its own inverse: (AB)B=A(A \oplus B) \oplus B = A
  • Same keystream used for both operations
  • No separate decryption function needed

Why ECB and CBC Cannot Do This:

In ECB/CBC:

  • Ciphertext is the direct output of AES
  • You must mathematically invert AES to decrypt
  • XOR alone cannot recover plaintext
  • Decryption requires the inverse function DKD_K

CTR avoids this by:

  • Never encrypting plaintext directly
  • Encrypting only counters (which are known values)
  • Plaintext is only XORed with keystream
  • Same XOR operation works both ways

Counter (CTR)

Counter Structure:

The counter is typically constructed as:

CTRj=IVjCTR_j = IV || j

Where:

  • IVIV = Initialization Vector (nonce)
  • jj = Block number (incremented sequentially)
  • || = Concatenation

Example:

  • IV = 0x00000000000000000000000000000001
  • Block 0: CTR_0 = IV || 0 = 0x00000000000000000000000000000001
  • Block 1: CTR_1 = IV || 1 = 0x00000000000000000000000000000002
  • Block 2: CTR_2 = IV || 2 = 0x00000000000000000000000000000003

Counter Requirements:

  • Must be unique for each block
  • Must never repeat with the same key
  • Typically: Nonce (IV) + block number
  • Nonce must be unique per message

Why CTR Is Powerful

1. No Chaining Dependency:

  • Each block encrypted independently
  • No dependency on previous blocks
  • Enables parallel processing

2. Keystream Can Be Precomputed:

  • Counter values known in advance
  • Keystream can be generated before plaintext arrives
  • Reduces latency

Generate the keystream (before plaintext exists):

CTR mode does not encrypt plaintext directly.

Instead, it encrypts counters:

KSj=EK(CTRj)KS_j = E_K(CTR_j)

BlockCounterEncrypted Counter (Keystream)
15KS1=EK(5)=1110KS_1 = E_K(5) = 1110
26KS2=EK(6)=0101KS_2 = E_K(6) = 0101
37KS3=EK(7)=1001KS_3 = E_K(7) = 1001

✔️ This can be done in advance
✔️ No plaintext needed
✔️ Fully parallelizable

3. Fully Parallelizable:

  • All blocks can be encrypted/decrypted simultaneously
  • Maximum performance on multi-core systems
  • Ideal for high-speed networks

4. No Pattern Leakage:

  • Each block uses unique counter value
  • Identical plaintext blocks produce different ciphertext
  • Secure against pattern analysis

5. Very Fast:

  • No chaining overhead
  • Parallel processing
  • Efficient for large data

📌 CTR mode behaves like a secure stream cipher when used correctly.

Security Considerations

Critical Requirements:

  • Counter must never repeat with the same key
  • Nonce must be unique for each message
  • Must be combined with authentication

Why Authentication Matters:

  • CTR provides confidentiality
  • CTR does not provide integrity
  • Attacker can modify ciphertext

Comparison Summary

ModeFormulaIV / CounterParallelizablePattern LeakageRemarks
ECBCj=EK(Pj)C_j = E_K(P_j)❌ No✅ Yes⚠️ YesInsecure
CBCCj=EK(PjCj1)C_j = E_K(P_j \oplus C_{j-1})✅ IV❌ No❌ NoTraditional
CTRCj=PjEK(CTRj)C_j = P_j \oplus E_K(CTR_j)✅ Counter✅ Yes❌ NoFast & modern

One-Line Summary

Block cipher modes determine how securely and efficiently data is encrypted, with CTR mode offering the best balance of performance and security when used correctly.