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.
These modes convert block ciphers into stream ciphers, making them suitable for real-time or streaming data.
Uses previous ciphertext as feedback for encryption. Stream-like behavior with limited error propagation.
Uses output of encryption function as feedback. Errors in transmission do not propagate.
Uses a counter value and nonce to generate a keystream. Highly parallelizable and modern.
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())
CFB Encrypted: acf1cc4b36924903be5aa8b5f76d6f09481f2d CFB Decrypted: GEC BHARUCH COLLEGE OFB Encrypted: ac72004859feb98aaebbb88e1d2d1b74871252 OFB Decrypted: GEC BHARUCH COLLEGE CTR Encrypted: ed27a73a5d5bf1eafc1d9bc4fb74b8e62c0aed CTR Decrypted: GEC BHARUCH COLLEGE
| 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 |
Secure messaging, real-time communication (e.g., chat apps)
Satellite communications, error-prone channels (bit error resilience)
VPNs, disk encryption, SSL/TLS, performance-sensitive tasks
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.
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.