Lesson 2 of 10
Symmetric Encryption
Symmetric encryption uses the same key to encrypt and decrypt. It is extremely fast, making it the choice for encrypting large amounts of data - files, database columns, and network traffic. AES (Advanced Encryption Standard) is the universal standard.
How Symmetric Encryption Works
- Both sender and receiver share a single secret key - the key must be kept confidential
- The same key encrypts and decrypts - faster than asymmetric encryption by orders of magnitude
- Key distribution problem: how do you securely share the key? (solved by asymmetric encryption in practice)
- AES (Advanced Encryption Standard): Selected by NIST in 2001, used by US government for classified data, universally adopted worldwide
- AES key sizes: 128-bit (secure), 192-bit (secure), 256-bit (recommended for highest security - NSA Suite B approved)
AES Encryption Modes
# Block cipher modes - how AES handles data larger than one block (16 bytes)
ECB (Electronic Codebook) - NEVER USE:
Each 16-byte block encrypted independently
PROBLEM: Identical plaintext blocks produce identical ciphertext blocks
Famous attack: The "ECB penguin" - encrypt a Linux Tux image with ECB
and the penguin outline remains visible in the ciphertext
CBC (Cipher Block Chaining) - Old, use with caution:
Each block XORed with previous ciphertext before encryption
Requires a random IV (Initialization Vector) per message
Vulnerable to padding oracle attacks (BEAST, POODLE) if implemented carelessly
GCM (Galois/Counter Mode) - RECOMMENDED:
Combines encryption with authentication (AEAD - Authenticated Encrypt with AD)
Detects tampering: decryption fails if ciphertext was modified
Used in TLS 1.3: AES-256-GCM
Parallelizable - efficient on modern hardware with AES-NI instructionsAES-256-GCM in Python
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os
# Generate a cryptographically random 256-bit key
key = os.urandom(32) # 32 bytes = 256 bits
# Create AES-GCM cipher
aesgcm = AESGCM(key)
# Encrypt
nonce = os.urandom(12) # 96-bit nonce - must be unique per encryption!
plaintext = b"Sensitive patient data: John Doe, DOB 1990-05-15"
aad = b"patient_record_v1" # Additional Authenticated Data (not encrypted, but authenticated)
ciphertext = aesgcm.encrypt(nonce, plaintext, aad)
print(f"Ciphertext ({len(ciphertext)} bytes): {ciphertext.hex()[:40]}...")
# Decrypt (same key, same nonce, same AAD required)
decrypted = aesgcm.decrypt(nonce, ciphertext, aad)
print(f"Decrypted: {decrypted.decode()}")
# If ciphertext is tampered, decryption raises InvalidTag exception
try:
tampered = bytes([ciphertext[0] ^ 0xFF]) + ciphertext[1:] # Flip first byte
aesgcm.decrypt(nonce, tampered, aad)
except Exception as e:
print(f"Tampering detected: {e}") # cryptography.exceptions.InvalidTagCommon Mistakes
- Reusing nonces/IVs - reusing a nonce with GCM catastrophically breaks security (nonce reuse attack can recover the key). Always generate a fresh random nonce per encryption operation
- Using ECB mode for anything - always use GCM or CTR. ECB is textbook-only
- Hardcoding encryption keys in source code - keys must be stored in environment variables, secrets managers (AWS Secrets Manager, HashiCorp Vault), or hardware security modules (HSM)
- Not using authenticated encryption - CBC without a MAC allows attackers to tamper with ciphertext without detection. Always use AEAD modes (GCM)
Tip
Tip
Practice Symmetric Encryption in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
AES for data. RSA for key exchange. bcrypt for passwords.
Practice Task
Note
Practice Task - (1) Write a working example of Symmetric Encryption from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Symmetric Encryption is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.
Key Takeaways
- Symmetric encryption uses the same key to encrypt and decrypt.
- Both sender and receiver share a single secret key - the key must be kept confidential
- The same key encrypts and decrypts - faster than asymmetric encryption by orders of magnitude
- Key distribution problem: how do you securely share the key? (solved by asymmetric encryption in practice)
Finished this lesson?