Symmetric encryption uses the same key to encrypt and decrypt. It is the fastest form of encryption and protects data at rest and in bulk data transfer. Today you master AES, understand modes of operation, and learn why key management is often harder than the crypto itself.
The Advanced Encryption Standard (AES) was selected by NIST in 2001 after an open international competition. It is a block cipher operating on 128-bit blocks with key sizes of 128, 192, or 256 bits. AES-256 has never been broken. It is used in TLS, disk encryption (BitLocker, FileVault, LUKS), and virtually every secure protocol. The NSA approves AES-256 for TOP SECRET data.
A mode of operation defines how AES handles data longer than 128 bits. ECB (Electronic Codebook) encrypts each block independently — identical plaintext blocks produce identical ciphertext, leaking patterns. CBC (Cipher Block Chaining) XORs each block with the previous ciphertext, hiding patterns. GCM (Galois/Counter Mode) provides authenticated encryption — it detects tampering. Always use AES-GCM or AES-CBC with HMAC.
Strong crypto fails with weak key management. Keys must be generated with a cryptographically secure random number generator (CSPRNG), never hardcoded in source code, stored in a key management system (AWS KMS, HashiCorp Vault), rotated regularly, and destroyed securely when retired. The most common crypto failure in real systems is poor key management, not weak algorithms.
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os
def encrypt(key: bytes, plaintext: bytes, aad: bytes = b'') -> tuple:
# GCM provides authenticated encryption
# Returns: (nonce, ciphertext+tag)
aesgcm = AESGCM(key)
nonce = os.urandom(12) # 96-bit nonce for GCM
ct = aesgcm.encrypt(nonce, plaintext, aad)
return nonce, ct
def decrypt(key: bytes, nonce: bytes, ct: bytes, aad: bytes = b'') -> bytes:
aesgcm = AESGCM(key)
# Raises InvalidTag if ciphertext was tampered with
return aesgcm.decrypt(nonce, ct, aad)
# Example usage
key = AESGCM.generate_key(bit_length=256)
message = b'Secret message for Precision AI Academy'
nonce, ciphertext = encrypt(key, message)
print(f'Ciphertext: {ciphertext.hex()[:32]}...')
recovered = decrypt(key, nonce, ciphertext)
print(f'Decrypted: {recovered.decode()}')
Demonstrate the ECB mode weakness by encrypting a bitmap image with AES-ECB and AES-CBC. The ECB version will reveal the image structure in the ciphertext. Post the two encrypted images side-by-side.