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

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:
Decryption:
Where:
- = j-th ciphertext block
- = j-th plaintext block
- = encryption function with key
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
Where:
- = j-th ciphertext block
- = j-th plaintext block
- = previous ciphertext block
- = encryption function with key
Process:
- XOR plaintext block with previous ciphertext block
- Encrypt the result
- Use this ciphertext for the next block's XOR
Decryption
Where:
- = decryption function with key
Process:
- Decrypt ciphertext block
- XOR with previous ciphertext block
- 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
- IV is a random value (same size as a block)
- IV ensures uniqueness even if plaintext repeats
Encryption with IV:
Decryption with IV:
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
Where:
- = j-th ciphertext block
- = j-th plaintext block
- = j-th counter value
- = encryption function with key
Process:
- Generate counter value
- Encrypt counter: (produces keystream)
- XOR plaintext with keystream:
Decryption
(The same operation works both ways.)
Why Decryption = Encryption:
- XOR is its own inverse:
- 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
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:
Where:
- = Initialization Vector (nonce)
- = 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:
| Block | Counter | Encrypted Counter (Keystream) |
|---|---|---|
| 1 | 5 | |
| 2 | 6 | |
| 3 | 7 |
✔️ 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
| Mode | Formula | IV / Counter | Parallelizable | Pattern Leakage | Remarks |
|---|---|---|---|---|---|
| ECB | ❌ No | ✅ Yes | ⚠️ Yes | Insecure | |
| CBC | ✅ IV | ❌ No | ❌ No | Traditional | |
| CTR | ✅ Counter | ✅ Yes | ❌ No | Fast & 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.