CMD Guide
HomeSystem DesignEncryption

What is Encryption

Encryption works by running your data through a reversible mathematical transformation that is cheap to compute if you hold a secret key and computationally infeasible to reverse if you do not — so the same bits look like uniform random noise to everyone except the key holder. Three nouns carry the whole idea: plaintext (the readable input), the key (the secret parameter), and ciphertext (the scrambled output). The algorithm itself is public; the key is the only thing that must stay secret. This is Kerckhoffs's principle — a cipher whose security depends on the algorithm staying hidden is already broken.

The cleanest way to see a key actually transform data is a stream cipher (how AES-CTR, AES-GCM, and ChaCha20 all work under the hood): the key seeds a pseudorandom keystream of bytes, and each plaintext byte is XORed with a keystream byte. XOR is its own inverse, so XORing again with the identical keystream recovers the plaintext.

diagram
diagram

Worked example: encrypt "HI" byte by byte

Take the two-byte plaintext "HI" and a keystream 2A C3 derived from a shared secret. Encryption is one XOR per byte; decryption is the same XOR again.

BytePlaintext (hex / binary)KeystreamCiphertext = P XOR K
00x48   0100 1000   'H'0x2A   0010 10100110 0010 = 0x62   'b'
10x49   0100 1001   'I'0xC3   1100 00111000 1010 = 0x8A

Ciphertext on the wire is 62 8A — no visible trace of "HI". To decrypt, XOR each ciphertext byte with the same keystream: 0x62 XOR 0x2A = 0x48 ('H') and 0x8A XOR 0xC3 = 0x49 ('I'). The key did all the work; the algorithm was public the whole time.

Why the naive "encryption gives integrity" claim is wrong

The original page said encryption helps verify data was not altered. It does not. Watch an attacker who never sees the key: they intercept C = 62 8A and flip the low bit of the first byte, 0x62 -> 0x63. The receiver decrypts as usual: 0x63 XOR 0x2A = 0100 1001 = 0x49 = 'I'. The message "HI" silently arrives as "II", and nothing in plain XOR/CTR/CBC ciphertext reveals the tampering. Because the transform is bit-for-bit linear, controlled edits to ciphertext become controlled edits to plaintext.

Integrity and authenticity come from a separate primitive: a MAC (message authentication code, e.g. HMAC) or, better, an AEAD cipher such as AES-256-GCM or ChaCha20-Poly1305 that produces an authentication tag over the ciphertext. The receiver recomputes the tag and rejects the message on any mismatch. Rule of thumb: encrypt-then-MAC, or just use an AEAD so you cannot get the composition wrong.

Symmetric vs. asymmetric — and a real asymmetric trace

Symmetric encryption (the XOR example above scaled up to AES) uses one shared key for both directions. It is fast — AES with hardware AES-NI runs at gigabytes per second — but both parties must already share the secret, which is the hard part on an open network.

Asymmetric encryption uses a public key to encrypt and a mathematically linked private key to decrypt, so the public key can be published freely. Toy RSA with key n=33, public e=3, private d=7: encrypt message m=4 as c = 4^3 mod 33 = 64 mod 33 = 31. Decrypt with the private exponent: 31^7 mod 33 = 4, recovering m. The security rests on factoring n being hard — trivial here, infeasible at real 2048–4096-bit sizes (and real RSA adds OAEP padding and can only encrypt payloads smaller than the key).

Encryption, hashing, and signatures — three different tools

These three primitives are often conflated, but they solve different problems:

This is why TLS needs both: the handshake uses asymmetric key agreement and signed certificates to establish identity, then symmetric AEAD encrypts the bulk data.

diagram
diagram

When to use which

Real systems almost never pick one — they go hybrid, which is exactly what TLS does: use asymmetric crypto once to agree on a fresh symmetric session key, then use fast symmetric AEAD for all the bulk traffic.

Pitfalls

Takeaways


Re-authored and deepened for this guide. Sources: Ferguson, Schneier & Kohno, Cryptography Engineering; Katz & Lindell, Introduction to Modern Cryptography; NIST SP 800-38D (AES-GCM); NIST SP 800-38A (block cipher modes); RFC 8446 (TLS 1.3) for the hybrid handshake; and Kerckhoffs's principle. The XOR/keystream trace, the malleability demonstration, and the toy RSA are hand-worked for this page.

Why the toy RSA exponents are inverses

For n = 33 = 3 × 11, Euler's totient counts the integers below 33 that are coprime to 33:

φ(33) = φ(3) × φ(11) = (3-1) × (11-1) = 2 × 10 = 20

The public exponent e = 3 and private exponent d = 7 are chosen so that:

e × d = 3 × 7 = 21 ≡ 1 (mod 20)

Euler's theorem says that for any m coprime to n:

mφ(n) ≡ 1 (mod n)

Therefore:

med = m1 + k·φ(n) = m × (mφ(n))k ≡ m × 1k ≡ m (mod n)

Check the toy example: encrypt m = 4 as c = 43 mod 33 = 64 mod 33 = 31. Decrypt as 317 mod 33. Since 317 = (3120)? · 317, and 3120 ≡ 1 (mod 33), the result collapses back to 4. The exponents are inverses in the multiplicative group modulo φ(n), not modulo n; that is the heart of RSA.

Homomorphic encryption frontier

Homomorphic encryption lets a third party compute on ciphertext and produce a ciphertext result that decrypts to the correct answer. A realistic use case is privacy-preserving medical analytics: a hospital encrypts patient records, sends the ciphertext to a cloud provider that runs an aggregation model, and receives an encrypted result. The cloud never sees plaintext; only the hospital's key can decrypt.

The practicality limits are severe. Fully homomorphic schemes are orders of magnitude slower than plaintext and need large ciphertext expansion. They are currently viable only for narrow, high-value workloads such as private set intersection, secure genomic computation, or limited federated learning. For routine cloud computation, traditional encryption-at-rest plus access controls remains the practical choice.

Key-management lifecycle

PhaseWhat happensTypical controls
GenerationCreate key with sufficient entropyHSM, CSPRNG, split knowledge for root keys
DistributionSecurely deliver key to consumersTLS, envelope encryption, KMS APIs, mTLS
UseEncrypt/decrypt/sign/verify dataRole-based access, audit logs, rate limits
RotationReplace old key with new keyGraceful re-encryption window, versioned ciphertext
Escrow / backupRecover from key lossShamir splitting, offline HSM backup, geographic separation
RevocationMark key as no longer validCRL, OCSP, KMS disable, denylist
DestructionSecurely erase key materialHSM zeroization, physical destruction for offline keys

Most cloud designs use envelope encryption: a data-encryption key (DEK) encrypts the payload, and a key-encryption key (KEK) protects the DEK. Rotating the KEK does not require re-encrypting petabytes of data — only the small DEK blobs are re-wrapped.

When NOT to roll your own crypto

Interviewer follow-ups & drills

  1. At rest vs in transit? Disk/KMS envelopes vs TLS on the wire — both usually required.
  2. Drill: why not ECB mode for files? Patterns leak; use GCM/CTR+MAC.
🤖 Don't fully get this? Learn it with Claude

Stuck on What is Encryption? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **What is Encryption** (System Design) and want to truly understand it. Explain What is Encryption from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **What is Encryption** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **What is Encryption** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **What is Encryption** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes