Overview
Curated: · Written: · Reviewed:
Encryption Fundamentals
In one line
Encryption gives confidentiality, MACs and AEAD tags give integrity, signatures give public verifiability, and hashes give one-way digests. Production systems use asymmetric crypto only to agree on or wrap keys, and symmetric AEAD to protect the data. State the property and the adversary before you name a primitive, because the adversary decides which primitive is enough.
Key definitions
- Confidentiality: only key holders can read the data. Delivered by AES, ChaCha20, RSA-OAEP and key agreement.
- Integrity: tampering is detectable. Delivered by HMAC, AEAD tags and signatures.
- AEAD: one construction that encrypts and tags together. Associated data (AAD) such as tenant ID is authenticated but not encrypted.
- Nonce: must never repeat under one key. Reuse exposes the XOR of two plaintexts and, for GCM, the authentication key.
- KDF: derives purpose-separated keys from high-entropy input (HKDF). It does not make a password strong.
- Envelope encryption: a data key encrypts the data, and a key-encryption key wraps the data key, so rotating the outer key does not re-encrypt the data.
Formulas and complexity
For $n$ random 96-bit nonces under one key, the collision probability is about
At $n = 2^{32}$ messages this is about $2^{-33}$, so rotate the data key well before that point. Quote AES-GCM throughput as multiple GB/s with AES-NI, and RSA-2048 public-key operations as thousands of times slower per operation than AES.
How it works
In TLS 1.3 the client and server each send an ephemeral key share. The ECDHE secret is fed through HKDF to derive per-direction traffic keys. The server signs the handshake transcript with its certificate key, which authenticates the server to the client. Records are then protected by AES-GCM or ChaCha20-Poly1305 with per-record nonces. The ephemeral keys give forward secrecy, and the client is authenticated only when mTLS is used.
When to use and when not to
| Need | Use | Avoid |
|---|---|---|
| Bulk data confidentiality | AES-256-GCM or ChaCha20-Poly1305 | ECB, or CBC without a MAC |
| Share a key with parties you have never met | Key agreement, or RSA-OAEP to wrap a symmetric key | RSA over the whole payload |
| Integrity between holders of one secret | HMAC | Public verification, since any verifier can forge |
| Public verification or release artifacts | Signatures such as Ed25519 or ES256 | HMAC for anything third parties must check |
| Password storage | Argon2id, or bcrypt or scrypt where already deployed | SHA-256 or any fast hash |
| Indexed lookup by a secret value | Keyed digest such as HMAC-SHA-256 under a server-held key | Randomized ciphertext, which never matches twice |
Traps interviewers probe
- Encrypting a lookup key with a random nonce. Each encryption produces different output, so the indexed lookup always misses. Fix: index a deterministic keyed digest of the presented key, and store the encrypted copy in a separate column if you need it.
- Repeating a GCM nonce. Fix: use random 96-bit nonces only within a counted message budget, or a counter that is guaranteed to be monotonic, and rotate the data key before the budget is spent.
- ECB, or CBC without a MAC. Fix: a high-level AEAD API with AAD that binds tenant and row ID, so a ciphertext cannot move to another record.
- Fast hashing for passwords, or running HKDF on a password. Fix: Argon2id with a unique salt per password. Use HKDF only on high-entropy keys.
- One key for several purposes. Fix: HKDF-Expand with a distinct info string per purpose, so the encryption key and the MAC key are never the same bytes.
Interview tips
Opening: name the property and the adversary first, then the primitive, then the key management. Interviewers score the reasoning, not the algorithm name.
Expected follow-ups:
- Why not RSA for everything? Throughput, ciphertext expansion, and the padding-oracle history of PKCS#1 v1.5.
- What happens if a nonce repeats? The XOR of the plaintexts leaks, and in GCM the authentication key can be recovered, so tags can be forged afterwards.
- HMAC or signature for JWTs? The deciding factor is who must verify. HS256 means every verifier holds the signing secret.
- Is TLS on enough? Only up to the termination point. Verify that certificate validation is on, and that internal hops are encrypted too.
- A key is compromised. Contain it, analyze exposure, replace the trust roots, and re-encrypt where the exposure requires it. Generating a new key is not enough.
30-second outline: The adversary is someone who can read the storage tier, not the network. I need confidentiality and integrity, so I use AES-256-GCM with a per-record nonce and tenant ID as AAD. The data key is wrapped by a KMS-held key, which gives envelope encryption. Every ciphertext records the key version, so rotation can run online. Asymmetric crypto appears only for key agreement or key wrapping. Authorization is a separate control and is still required.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.