5.3 - Protecting Stored Data with Cryptography
Encryption Fundamentals
Cryptography is the practice of hiding information to protect confidentiality. Encryption converts readable information into unreadable form, and decryption reverses that process to get the original back. The original readable data is called plaintext, the scrambled output is ciphertext, the secret value driving the process is the key, and the algorithm is the mathematical process defining exactly how encryption and decryption happen.
For example, plaintext like "Meet at noon" combined with a key and run through an algorithm like AES produces unreadable ciphertext, and running that ciphertext back through with the correct key restores the original plaintext.
Keyspace
The keyspace is the total number of possible keys an algorithm could use. For an n-bit key, that's 2^n possibilities. An 8-bit key has just 256 possible keys (crackable instantly), a 128-bit key has about 3.4 x 10^38 possibilities (would take roughly 10^21 years to brute force even at a billion guesses a second, far longer than the universe has existed), and a 256-bit key has about 1.1 x 10^77 possibilities (an almost incomprehensibly long brute-force time). This is why 128-bit keys are considered sufficient for most purposes, with 256-bit reserved for the highest security needs.
Encryption Algorithm Classifications
Symmetric encryption uses the same key to both encrypt and decrypt. It's fast and great for bulk data, disk encryption, and file encryption, but it has a real challenge: how do you securely get that shared key to the other party in the first place? Examples include AES, DES, and 3DES.
Asymmetric encryption uses a mathematically related key pair, a public key for encrypting and a private key for decrypting. It's slower than symmetric encryption but solves the key distribution problem completely, since the public key can be shared openly with anyone; only the matching private key can decrypt what was encrypted with it. Examples include RSA and ECC (Elliptic Curve Cryptography). In practice, most real systems use asymmetric encryption just to securely exchange a symmetric key, then switch to fast symmetric encryption for the actual bulk data, getting the best of both worlds.
Block encryption processes data in fixed-size chunks, like AES working on 128-bit blocks at a time, padding the last block if the data doesn't divide evenly. Stream encryption processes data continuously, one bit or byte at a time, generating a keystream and XORing it against the plaintext, which is faster for real-time data and needs no padding. Block encryption suits file and disk encryption and most general use, while stream encryption suits real-time communications, video streaming, and some wireless protocols.
Advanced Encryption Standard (AES)
AES is the most widely used symmetric encryption algorithm in the world, chosen by NIST in 2000 and made an official standard in 2001 to replace the older DES standard. It's everywhere: Wi-Fi security (WPA2/WPA3), HTTPS/TLS, disk encryption tools like BitLocker and FileVault, processor-level encryption, VPNs, encrypted messaging, and database encryption.
Technically, AES is a symmetric block cipher with a fixed 128-bit block size, but it comes in three key length variants. AES-128 (128-bit key) is fastest and sufficient for most general purposes like file encryption and web traffic. AES-192 (192-bit key) offers enhanced security at moderate speed, mostly used in government and military contexts. AES-256 (256-bit key) is the slowest, needing roughly 40% more processing time than AES-128, but offers maximum security, appropriate for top secret data and long-term sensitive storage, and it's still extremely fast on modern hardware with dedicated AES instruction support.
Like every computer-based encryption algorithm, AES works on binary data. A text file, a photo, and a spreadsheet are all just strings of bits to it, cut into 128-bit (16-byte) blocks. The block size stays 128 bits no matter which key length you pick; a longer key makes the encryption more secure but takes more time to encrypt and decrypt.
At a high level, AES works by expanding the original key into a set of round keys, then running the data through multiple rounds (10 for AES-128, 12 for AES-192, 14 for AES-256) of substitution, permutation, and mixing steps combined with those round keys, with each round increasing both confusion (obscuring the relationship between key and ciphertext) and diffusion (spreading each bit of plaintext's influence widely across the output).
Practical AES Implementation
You can encrypt with AES through command-line tools like OpenSSL or GPG, specialized software like VeraCrypt or 7-Zip, or web-based tools (though those carry real risk for sensitive data since you're trusting an unknown third party with your plaintext).
Another example of specialized software is AES Crypt, a program made just for encrypting and decrypting files with AES through a simple app instead of the command line.
Using OpenSSL, a typical AES-128 encryption command looks like
openssl enc -aes-128-cbc -e -in confidential.txt -k MySecurePass123! -out confidential.txt.enc, specifying the algorithm and mode, the encrypt flag, the input file, a password (which the key gets derived from), and the output file. Decryption just swaps the -e for -d and swaps the input and output files. For stronger security, swapping in -aes-256-cbc instead uses AES-256, the only real difference in the command being that algorithm flag.Here are the two commands for a file named
test, using AES with a 128-bit key:openssl enc -aes-128-cbc -e -in test -k password -out test.enc openssl enc -aes-128-cbc -d -in test.enc -k password -out test_decrypted
Current versions of OpenSSL also print this warning for both commands:
*** WARNING : deprecated key derivation used. Using -iter or -pbkdf2 would be better.
The file still gets encrypted and decrypted correctly. The warning is about how the key gets made from the password, which is covered below.
CBC (Cipher Block Chaining) mode works by XORing each plaintext block with the previous block's ciphertext before encrypting (the very first block uses a random initialization vector, or IV, instead), which means identical plaintext blocks don't produce identical ciphertext, making patterns much harder to spot. Other modes exist too, like ECB (considered insecure, since identical plaintext blocks do produce identical ciphertext), CTR, and GCM (which adds authentication and is generally preferred for new systems).
A full workflow example: encrypting a confidential report before emailing it might look like verifying the file exists and is readable, encrypting it with
openssl enc -aes-256-cbc -e -in report.docx -k "..." -out report.docx.enc, confirming the output is genuinely unreadable binary data, sending the encrypted file over email (safe since it's ciphertext), but sharing the actual password through a completely separate channel like a phone call or a different secure messaging app, never in the same email as the encrypted file itself. The recipient then decrypts using the matching command and password to get back the original file.A few important considerations: password strength matters just as much as the algorithm itself, since a weak password like "password123" means an attacker just brute forces the password rather than the actual 256-bit key, completely undermining the encryption's strength. The OpenSSL commands above turn your password into the key with a quick, older method (that's what the warning is about). Adding
-pbkdf2 to both the encrypt and decrypt commands switches to PBKDF2, which adds computational cost specifically to slow down brute-force attempts. Never share the password alongside the encrypted file itself, always use a separate channel, or better yet use proper public-key encryption like PGP/GPG for exchanging keys in the first place. After encrypting something, always verify you can successfully decrypt it before securely deleting (not just regular-deleting) the original plaintext, and store your encryption keys or passwords in something like a password manager rather than writing them down somewhere insecure. Basic OpenSSL encryption like this has real limitations too, like no built-in way to verify a file hasn't been tampered with, and being entirely password-based means it's only as strong as the password chosen; for production systems, proper tools like GPG, full disk encryption, enterprise key management, or hardware security modules are generally a better fit.Practical use cases for this kind of encryption include full disk encryption protecting a stolen laptop's data (the drive is unreadable without the login-derived decryption key), encrypting files locally before uploading to cloud storage so the cloud provider only ever sees ciphertext, encrypting email attachments before sending since email travels through multiple unencrypted servers along the way, and encrypting the contents of a USB drive so a lost or stolen drive only exposes useless encrypted data to whoever finds it.