Lesson 8 of 10
Encryption in Practice
Applied cryptography - encrypting files, columns in databases, emails, and understanding VPN protocols - bridges the gap between theory and the real-world tasks security engineers perform daily.
Disk & File Encryption
# Full disk encryption protects data when a device is physically stolen
# LUKS (Linux Unified Key Setup) - standard disk encryption on Linux
# Setup (WARNING: destroys data on disk):
cryptsetup luksFormat /dev/sdb # Initialize LUKS container
cryptsetup luksOpen /dev/sdb encrypted_disk # Unlock
mkfs.ext4 /dev/mapper/encrypted_disk # Format
mount /dev/mapper/encrypted_disk /mnt/secure # Mount
# Check LUKS info:
cryptsetup luksDump /dev/sdb
# Shows: cipher (aes-xts-plain64), key size (256/512), UUID
# File-level encryption with GPG (GNU Privacy Guard)
# Generate key pair
gpg --gen-key
# Encrypt file for a recipient
gpg --encrypt --recipient alice@example.com sensitive_report.pdf
# Encrypt with password (symmetric)
gpg --symmetric --cipher-algo AES256 sensitive.txt
# Creates: sensitive.txt.gpg
# Decrypt
gpg --decrypt sensitive.txt.gpg
# Encrypt with openssl (simple file encryption)
openssl enc -aes-256-gcm -in plaintext.txt -out encrypted.bin -pbkdf2
openssl enc -d -aes-256-gcm -in encrypted.bin -out decrypted.txt -pbkdf2Database Column Encryption
# Encrypting sensitive database columns (Python + SQLAlchemy example)
from cryptography.fernet import Fernet
import os
# Key management: load from environment variable, never hardcode
ENCRYPTION_KEY = os.environ.get('DB_ENCRYPTION_KEY')
if not ENCRYPTION_KEY:
raise ValueError("DB_ENCRYPTION_KEY environment variable not set")
cipher = Fernet(ENCRYPTION_KEY.encode())
class UserRecord:
def __init__(self, name, ssn, credit_card):
self.name = name # Store plaintext (not sensitive)
self.ssn_encrypted = cipher.encrypt(ssn.encode()) # Encrypt SSN
self.card_encrypted = cipher.encrypt(credit_card.encode()) # Encrypt card
def get_ssn(self):
return cipher.decrypt(self.ssn_encrypted).decode()
# What to encrypt in databases:
# MUST ENCRYPT: SSN, credit cards, health records, passwords (hash instead), biometrics
# ENCRYPT: Email addresses, phone numbers, addresses (GDPR/HIPAA often requires this)
# MAY LEAVE PLAINTEXT: Username, non-sensitive preferences, public profile data
#
# Encryption at rest (full database): Use PostgreSQL TDE, AWS RDS encryption,
# Azure Transparent Data Encryption - encrypts the entire database file
# Key management best practices:
# Never store encryption keys in the same database as encrypted data
# Use AWS KMS, Azure Key Vault, or HashiCorp Vault for key management
# Rotate keys annually or on compromiseVPN Protocols Comparison
- OpenVPN: Mature, widely supported, runs over TCP or UDP. Uses TLS for control channel, configurable ciphers. Cross-platform. Widely trusted for corporate VPNs
- WireGuard: Modern (2020), fast, minimal codebase (~4,000 lines vs OpenVPN's ~400,000). Uses ChaCha20, Poly1305, Curve25519. Built into Linux kernel 5.6+. Recommended for new deployments
- IPSec/IKEv2: Native support in most OS without additional software. Used for mobile VPNs (iOS, Android native). Strong security when properly configured
- Deprecated/Avoid: PPTP (broken, MS-CHAPv2 cracked), L2TP without IPSec (no encryption), SSL VPNs with SSLv3 or TLS 1.0
Common Mistakes
- Encrypting without key management - encrypting data and storing the key in the same database row provides no security benefit
- Using ECB mode for disk encryption - disk encryption uses XTS mode (specifically designed for disk sectors); never use ECB
- Not encrypting database backups - backups of encrypted databases are often stored unencrypted. Breaching the backup is equivalent to breaching the database
- Forgetting encryption at rest when moving to cloud - cloud storage (S3, Azure Blob) is not encrypted by default in all configurations; always enable encryption at rest
Tip
Tip
Practice Encryption in Practice in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
The foundational security model: Confidentiality (privacy), Integrity (trust), and Availability (uptime).
Practice Task
Note
Practice Task - (1) Write a working example of Encryption in Practice 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 Encryption in Practice 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
- Applied cryptography - encrypting files, columns in databases, emails, and understanding VPN protocols - bridges the gap between theory and the real-world tasks security engineers perform daily.
- OpenVPN: Mature, widely supported, runs over TCP or UDP. Uses TLS for control channel, configurable ciphers. Cross-platform. Widely trusted for corporate VPNs
- WireGuard: Modern (2020), fast, minimal codebase (~4,000 lines vs OpenVPN's ~400,000). Uses ChaCha20, Poly1305, Curve25519. Built into Linux kernel 5.6+. Recommended for new deployments
- IPSec/IKEv2: Native support in most OS without additional software. Used for mobile VPNs (iOS, Android native). Strong security when properly configured
Finished this lesson?