ECB is the simplest and most straightforward mode of block cipher operation. It divides the plaintext into blocks and encrypts each block independently using the same key. However, it reveals patterns in the plaintext because identical plaintext blocks produce identical ciphertext blocks.
plaintext ──► [ Encrypt with K ] ──► ciphertext
P₁ ──► E(K,·) ──► C₁
P₂ ──► E(K,·) ──► C₂
P₃ ──► E(K,·) ──► C₃
ciphertext ──► [ Decrypt with K ] ──► plaintext
C₁ ──► D(K,·) ──► P₁
C₂ ──► D(K,·) ──► P₂
C₃ ──► D(K,·) ──► P₃
CBC improves on ECB by introducing chaining between blocks. Each plaintext block is XORed with the previous ciphertext block before encryption. This hides patterns in plaintext, achieving IND-CPA security. A random IV is used for the first block.
IV
│
P₁ ──⊕──┘
│
[E]──► C₁
│
P₂ ──⊕◄──────┘
│
[E]──► C₂
│
P₃ ──⊕◄──────┘
│
[E]──► C₃
IV
│
[D]◄── C₁
│
P₁ ◄─⊕──┘
│
[D]◄───── C₂
│
P₂ ◄─⊕──┘
│
[D]◄───── C₃
│
P₃ ◄─⊕──┘
CFB mode turns a block cipher into a self-synchronizing stream cipher. The encryption of the previous ciphertext block (or IV) is XORed with the current plaintext block to produce the ciphertext. Only encryption function is used.
IV
│
[E]──►
│
P₁ ──⊕──┘──► C₁
│
[E]◄──
│
P₂ ──⊕───────┘──► C₂
│
[E]◄──
│
P₃ ──⊕───────────┘──► C₃
IV
│
[E]──►
│
C₁ ──⊕──┘──► P₁
│
[E]◄──
│
C₂ ──⊕───────┘──► P₂
│
[E]◄──
│
C₃ ──⊕───────────┘──► P₃
OFB mode transforms a block cipher into a synchronous stream cipher. It repeatedly encrypts an internal state (starting from IV), and XORs it with the plaintext. It avoids ciphertext feedback and ensures that transmission errors do not propagate.
IV
│
[E]──► O₁
│
P₁ ──⊕───────┘──► C₁
│
[E]──► O₂
│
P₂ ──⊕───────┘──► C₂
│
[E]──► O₃
│
P₃ ──⊕───────┘──► C₃
IV
│
[E]──► O₁
│
C₁ ──⊕───────┘──► P₁
│
[E]──► O₂
│
C₂ ──⊕───────┘──► P₂
│
[E]──► O₃
│
C₃ ──⊕───────┘──► P₃
CTR mode converts a block cipher into a fully parallelizable stream cipher. It generates keystream blocks by encrypting successive values of a counter combined with a nonce. The keystream is XORed with plaintext to encrypt and with ciphertext to decrypt.
Nonce∥0 ──► [E]──► O₀ ──⊕──► C₀
▲
P₀
Nonce∥1 ──► [E]──► O₁ ──⊕──► C₁
▲
P₁
Nonce∥2 ──► [E]──► O₂ ──⊕──► C₂
▲
P₂
Nonce∥0 ──► [E]──► O₀ ──⊕──► P₀
▲
C₀
Nonce∥1 ──► [E]──► O₁ ──⊕──► P₁
▲
C₁
Nonce∥2 ──► [E]──► O₂ ──⊕──► P₂
▲
C₂
GCM provides authenticated encryption by combining CTR-mode encryption with a GHASH-based authentication tag. It ensures both confidentiality and integrity of the data, making it suitable for modern protocols like TLS.
Nonce
│
Counter₀ ───► [E]──► O₀ ──⊕──► C₀
Counter₁ ───► [E]──► O₁ ──⊕──► C₁
Counter₂ ───► [E]──► O₂ ──⊕──► C₂
...
C₀,C₁,C₂,...,AAD
│
GHASH
│
Tag (Authentication)