AES Encryption
Advanced Encryption Standard: blocks, keys, and rounds
Skills taught: crypto.aes
The Advanced Encryption Standard
AES is the global standard for symmetric (shared-key) encryption. It's used in TLS, full-disk encryption, Wi-Fi (WPA2/3), and countless storage systems. The same key encrypts and decrypts, making key distribution the main challenge.
- Block size: always 128 bits (16 bytes) — fixed regardless of key size
- Key sizes: 128-bit (AES-128), 192-bit (AES-192), 256-bit (AES-256)
- Rounds: 10 / 12 / 14 for 128 / 192 / 256-bit keys
- No known practical attacks after 20+ years of cryptanalysis
- Block Cipher
- An encryption algorithm that operates on fixed-size blocks. AES processes exactly 16 bytes at a time. Longer messages require a mode of operation to chain blocks.
AES-CBC: The Classic Mode
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
import os
key = os.urandom(32) # AES-256: 32-byte key
iv = os.urandom(16) # 128-bit IV, one per message
# --- Encrypt ---
def aes_cbc_encrypt(key: bytes, iv: bytes, plaintext: bytes) -> bytes:
# PKCS7 padding to align to 16 bytes
padder = padding.PKCS7(128).padder()
padded = padder.update(plaintext) + padder.finalize()
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
encryptor = cipher.encryptor()
return encryptor.update(padded) + encryptor.finalize()
# --- Decrypt ---
def aes_cbc_decrypt(key: bytes, iv: bytes, ciphertext: bytes) -> bytes:
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
decryptor = cipher.decryptor()
padded = decryptor.update(ciphertext) + decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
return unpadder.update(padded) + unpadder.finalize()
# Usage
msg = b"Top secret message"
ciphertext = aes_cbc_encrypt(key, iv, msg)
print("Encrypted:", ciphertext.hex())
print("Decrypted:", aes_cbc_decrypt(key, iv, ciphertext))
Warning: AES-CBC does not authenticate the ciphertext. An attacker can modify the ciphertext undetected and potentially decrypt it via a padding oracle attack. For new code, use AES-GCM instead.
AES-GCM: The Recommended Mode
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os
key = AESGCM.generate_key(bit_length=256) # 32-byte random key
nonce = os.urandom(12) # 12-byte nonce (96-bit, recommended for GCM)
aad = b"version:1,user:42" # optional associated data (authenticated but not encrypted)
aesgcm = AESGCM(key)
# Encrypt — produces ciphertext + 16-byte auth tag appended
ciphertext = aesgcm.encrypt(nonce, b"Sensitive data", aad)
# Decrypt — raises InvalidTag if anything was tampered with
try:
plaintext = aesgcm.decrypt(nonce, ciphertext, aad)
print("Decrypted:", plaintext)
except Exception:
print("Tampering detected — authentication failed!")
AES-CBC vs AES-GCM
- AES-CBC: Classic mode, encryption only
- AES-GCM: Modern AEAD mode — encryption + authentication
Tip: AES-256 provides ~128-bit post-quantum security (Grover's algorithm halves effective key size). This makes AES-256 a future-proof choice even when quantum computers exist.
Frequently Asked Questions
Should I use AES-128 or AES-256?
AES-256 is recommended for new code. AES-128 has no known practical break, but AES-256 provides extra margin and post-quantum safety (128-bit quantum security vs 64-bit for AES-128).
Should I use AES-CBC in new code?
No. Use AES-GCM. AES-CBC does not authenticate the ciphertext, making it vulnerable to padding oracle attacks when used with PKCS7 padding and error messages.
Does AES have any known weaknesses?
No practical attacks exist. The best known attack (biclique) reduces effective key size by just 2 bits. After 20+ years of cryptanalysis, AES remains the gold standard.