Practical 5: AES Encryption Modes

Compare CFB, OFB, and CTR Modes using AES

🎯Objective

To implement and compare AES encryption and decryption using CFB (Cipher Feedback), OFB (Output Feedback), and CTR (Counter) modes, and analyze their behavior, security, and performance characteristics.

📚Theoretical Background

These modes convert block ciphers into stream ciphers, making them suitable for real-time or streaming data.

🔄 CFB (Cipher Feedback)

Uses previous ciphertext as feedback for encryption. Stream-like behavior with limited error propagation.

🔃 OFB (Output Feedback)

Uses output of encryption function as feedback. Errors in transmission do not propagate.

📊 CTR (Counter)

Uses a counter value and nonce to generate a keystream. Highly parallelizable and modern.

💻Python Implementation

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad

key = get_random_bytes(16)  # 128-bit AES key
plaintext = b'GEC BHARUCH COLLEGE'
iv = get_random_bytes(16)

# CFB Mode
cipher_cfb = AES.new(key, AES.MODE_CFB, iv)
ct_cfb = cipher_cfb.encrypt(plaintext)
pt_cfb = AES.new(key, AES.MODE_CFB, iv).decrypt(ct_cfb)

# OFB Mode
cipher_ofb = AES.new(key, AES.MODE_OFB, iv)
ct_ofb = cipher_ofb.encrypt(plaintext)
pt_ofb = AES.new(key, AES.MODE_OFB, iv).decrypt(ct_ofb)

# CTR Mode
cipher_ctr = AES.new(key, AES.MODE_CTR)
ct_ctr = cipher_ctr.encrypt(plaintext)
pt_ctr = AES.new(key, AES.MODE_CTR, nonce=cipher_ctr.nonce).decrypt(ct_ctr)

print("CFB Encrypted:", ct_cfb.hex())
print("CFB Decrypted:", pt_cfb.decode())

print("\nOFB Encrypted:", ct_ofb.hex())
print("OFB Decrypted:", pt_ofb.decode())

print("\nCTR Encrypted:", ct_ctr.hex())
print("CTR Decrypted:", pt_ctr.decode())

📤Sample Output

CFB Encrypted: acf1cc4b36924903be5aa8b5f76d6f09481f2d
CFB Decrypted: GEC BHARUCH COLLEGE

OFB Encrypted: ac72004859feb98aaebbb88e1d2d1b74871252
OFB Decrypted: GEC BHARUCH COLLEGE

CTR Encrypted: ed27a73a5d5bf1eafc1d9bc4fb74b8e62c0aed
CTR Decrypted: GEC BHARUCH COLLEGE

⚖️Mode Comparison

Feature CFB OFB CTR
IV Required ✅ Yes ✅ Yes ❌ No (uses nonce)
Keystream Reuse Danger ✅ Yes ✅ Yes ✅ Yes
Error Propagation Affects current + next block None None
Parallel Encryption ❌ No ❌ No ✅ Yes
Use Case Secure I/O devices Wireless communication VPNs, TLS, disk encrypt

🌐Real-Life Applications

CFB Mode

Secure messaging, real-time communication (e.g., chat apps)

OFB Mode

Satellite communications, error-prone channels (bit error resilience)

CTR Mode

VPNs, disk encryption, SSL/TLS, performance-sensitive tasks

🔐Security Aspects

  • All modes are semantically secure when implemented correctly
  • Vulnerable if IVs or nonces are reused
  • CFB & OFB are sequential — harder to parallelize
  • CTR is parallelizable, faster, and ideal for high-speed applications
  • These modes hide patterns in plaintext better than ECB

🔍 Key Observations

CFB: Uses ciphertext chaining — slight change in input affects all output.

OFB: Independent of ciphertext, uses feedback loop of keystream — bit-flip error in ciphertext doesn't propagate.

CTR: Generates keystream using counters — highly efficient and parallelizable.

📝Important Notes

🎓Learning Outcomes

  • Understand the use of stream cipher modes in AES
  • Learn how to implement CFB, OFB, and CTR modes in Python
  • Compare security, performance, and error behavior across modes
  • Know the importance of IV and nonce in encryption
  • Realize how different modes affect data integrity and performance

📋Summary

This practical explores three stream cipher modes of AES (CFB, OFB, CTR) and highlights how they offer flexibility, efficiency, and security for different real-world needs. Each mode transforms AES from block to stream behavior without requiring padding, ensuring smooth operation on variable-length data. While all provide confidentiality, CTR mode stands out for speed and parallelizability, making it the preferred choice for modern encryption needs like VPNs, cloud storage, and fast web protocols.