5. Symmetric Key Encryption (AES-256-GCM)
Introduction to Symmetric Encryption
Symmetric encryption is a cryptographic primitive where the exact same key is used for both the encryption of plaintext into ciphertext and the decryption of ciphertext back into plaintext. Because both parties must possess the same secret key, key distribution security is the primary challenge.
In a symmetric system, we take a plaintext input (P) and a key (K) and apply an algorithm to produce ciphertext (C). To retrieve the data, the operation is reversed: P = Decrypt(C, K). The security relies entirely on the secrecy of K, not the obscurity of the algorithm.
openssl enc -aes-256-cbc -salt -in data.txt -out data.enc -k 'super-secret-key'; openssl enc -d -aes-256-cbc -in data.enc -out decrypted.txt -k 'super-secret-key'
Stream vs Block Ciphers
Ciphers are categorized based on how they process data. Block ciphers take a fixed-size block of bits (e.g., 128-bit) and transform them using the key. Stream ciphers treat the plaintext as a continuous stream of bits, XORing them with a keystream.
| Cipher Type | Mechanism | Examples |
|---|---|---|
| Block Cipher | Processes fixed-size blocks | AES, DES |
| Stream Cipher | Processes bit-by-bit via XOR | ChaCha20, RC4 |
import os; from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305; key = ChaCha20Poly1305.generate_key(); cipher = ChaCha20Poly1305(key); nonce = os.urandom(12); ciphertext = cipher.encrypt(nonce, b'Sensitive Data', None); print(f'Ciphertext (hex): {ciphertext.hex()}')
Advanced Encryption Standard (AES)
AES is the industry standard for symmetric block encryption. It operates on a fixed block size of 128 bits. The security strength is determined by the key length: 128, 192, or 256 bits. AES-256 is the gold standard for high-security environments.
The algorithm uses a substitution-permutation network. During encryption, the data undergoes multiple rounds of SubBytes, ShiftRows, MixColumns, and AddRoundKey operations. A 256-bit key involves 14 rounds of these transformations.
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes; import os; key = os.urandom(32); iv = os.urandom(16); cipher = Cipher(algorithms.AES(key), modes.CBC(iv)); encryptor = cipher.encryptor(); ct = encryptor.update(b'VoidX-Data-Packet') + encryptor.finalize(); print(f'Encrypted block: {ct.hex()}')
Authenticated Encryption: AES-GCM
AES-GCM (Galois/Counter Mode) provides both confidentiality and authenticity. Unlike older modes (CBC/ECB), it detects if the ciphertext has been tampered with by including a GMAC (Galois Message Authentication Code).
GCM is classified as AEAD (Authenticated Encryption with Associated Data). This means you can authenticate cleartext data (like packet headers) alongside the encrypted payload, preventing man-in-the-middle attacks from modifying packet metadata.
from cryptography.hazmat.primitives.ciphers.aead import AESGCM; key = AESGCM.generate_key(bit_length=256); aesgcm = AESGCM(key); nonce = os.urandom(12); data = b'Critical Payload'; ad = b'Header-Info'; ct = aesgcm.encrypt(nonce, data, ad); print(f'Authenticated Ciphertext: {ct.hex()}')
Knowledge Check
Ready to test your understanding of 5. Symmetric Key Encryption (AES-256-GCM)?