Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 40 ยท Practice and Review

Cryptography Interview Questions

Prepare for cryptography interviews: symmetric vs asymmetric, hash vs MAC, why salt, forward secrecy, RSA vs ECC and more, with short answers and runnable code.

Cryptography questions in software interviews rarely ask you to invent an algorithm. They test whether you understand which tool solves which problem, why common designs are built the way they are, and whether you can spot a dangerous shortcut. A good answer is short and structured: name the idea, give the reason, and mention the pitfall.

This page collects the questions that come up most often, grouped by topic. Each answer is deliberately brief so you can say it aloud in under a minute. Three questions also come with runnable code, because showing a small demonstration is often the strongest answer. Use the links to the earlier lessons when an answer needs more depth.

flowchart TD
    Q["Interview question"] --> A["1. Name the goal:<br/>secrecy, integrity, authenticity"]
    A --> B["2. Name the right primitive"]
    B --> C["3. Say why alternatives fail"]
    C --> D["4. Mention one pitfall"]

A four-step shape for answering almost any cryptography question.

Fundamentals

What are the main goals of cryptography? Confidentiality (only the intended party can read), integrity (changes are detected), authentication (you know who sent it) and non-repudiation (the sender cannot deny it). Most primitives deliver one or two of these, never all four (what is cryptography).

What is Kerckhoffs's principle? A system should stay secure even if everything about it except the key is public. Security through obscurity of the algorithm is fragile; secret keys can be changed, secret algorithms cannot.

What is the difference between encoding, hashing and encryption? Encoding (base64, hex) changes representation and is reversible by anyone. Hashing is one-way and keyless. Encryption is reversible only with the key (encoding vs encryption).

Symmetric vs asymmetric encryption: what is the difference and when do you use each? Symmetric encryption uses one shared secret key; it is fast and suits bulk data, but the parties must somehow share the key. Asymmetric encryption uses a public/private key pair; it solves key distribution and enables signatures but is slow and limited in message size. Real systems are hybrid: asymmetric methods agree on a key, and a symmetric cipher encrypts the data (public-key ideas).

What is a one-time pad and why is it not used everywhere? XOR with a truly random key as long as the message, used once. It has perfect secrecy, but the key must be as long as the message, random, secret, and never reused, which makes distribution impractical (XOR and the one-time pad).

What is a nonce, an IV and a salt? All are public values that make operations unique. A nonce ("number used once") must not repeat under the same key; an IV is the initial value of a mode (random for CBC, unique for CTR/GCM); a salt randomizes password hashes. None of them is secret.

Why must you never use random for security? It is a deterministic generator designed for simulations; its output can be predicted from the seed or recovered from observed values. Use secrets or os.urandom, which draw from the operating system's cryptographically secure generator (randomness and entropy).

Hashing, MACs and signatures

What properties does a cryptographic hash have? Preimage resistance (cannot find an input for a given hash), second-preimage resistance (cannot find another input with the same hash as a given one) and collision resistance (cannot find any two inputs with the same hash). A small input change flips about half the output bits (the avalanche effect) (hash functions).

What is the birthday bound and why does it matter? Finding some collision in an n-bit hash takes about 2^(n/2) tries, not 2^n. That is why 128-bit hashes (MD5) are broken for collisions and why SHA-256 gives about 128 bits of collision security (hash attacks).

What is the difference between a hash, a MAC and a digital signature? A hash has no key: anyone can compute it. A MAC uses a shared secret key and proves a message came from someone holding the key and was not changed. A signature uses a private key and anyone with the public key can verify it, which gives non-repudiation.

Python Hash vs MAC: who can forge?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Why use HMAC instead of hash(key + message)? Hashes built with the Merkle-Damgard design (MD5, SHA-1, SHA-256) allow length-extension: given H(key + m) and the length of the key, an attacker can compute a valid tag for m + padding + extra without the key. HMAC's nested construction blocks that (MACs and HMAC).

Why compare MAC tags with hmac.compare_digest? Ordinary comparison returns at the first differing byte, so response time reveals how many bytes matched, and an attacker can build the tag one byte at a time. compare_digest takes the same time either way.

Why hash then sign, instead of signing the message directly? Signing is slow and RSA can only process a number smaller than the modulus. Hashing first lets you sign messages of any length, and a collision-resistant hash makes signing the digest as strong as signing the message (digital signatures).

Passwords and keys

How should passwords be stored? With a slow, memory-hard or iterated password hashing function (Argon2id, scrypt, bcrypt or PBKDF2) and a unique random salt per user, with a work factor you can raise over time. Never store them in plain text, encrypted with a reversible key, or hashed with fast functions like MD5 or plain SHA-256 (password hashing).

Why salt? A salt makes equal passwords hash differently and makes precomputed tables (rainbow tables) useless, so an attacker must attack each user separately. A pepper is an additional secret kept outside the database.

Python What a salt changes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

What is a key derivation function (KDF)? A function that turns a secret (a password, or a shared Diffie-Hellman value) into one or more strong keys. Password KDFs are deliberately slow; HKDF is fast and extracts and expands keys from material that already has high entropy (key derivation).

How do you manage keys in production? Keep them out of source control, store them in a KMS or secret manager, give each environment and purpose its own key, restrict access, log use, and rotate on a schedule and after any suspected leak. Design so that rotation works without downtime.

Public-key cryptography

How does RSA work in one minute? Pick two large primes p and q, publish n = p*q and an exponent e, and keep d, the inverse of e modulo (p-1)(q-1). Encrypt with c = m^e mod n and decrypt with m = c^d mod n. Security rests on the difficulty of factoring n (RSA from scratch).

Why is textbook RSA insecure? It is deterministic (equal messages give equal ciphertexts), malleable (multiplying ciphertexts multiplies plaintexts) and breaks for small messages with small exponents. Real use needs OAEP for encryption and PSS for signatures (RSA padding and pitfalls).

RSA vs ECC: how do they compare? Both give public-key encryption-style operations and signatures. ECC reaches the same security with much smaller keys and faster operations, because the best known attacks on elliptic-curve discrete logarithms are far weaker than attacks on factoring (elliptic curves).

Security level (approx.) Symmetric key RSA modulus Elliptic curve
112 bits 112 2048 224
128 bits 128 3072 256 (P-256, X25519)
192 bits 192 7680 384
256 bits 256 15360 521

What is Diffie-Hellman and what does it not do? Two parties agree on a shared secret over an open channel by exchanging g^a and g^b. It gives key agreement but no authentication, so a man in the middle can run two exchanges unless the values are signed or otherwise authenticated (Diffie-Hellman).

Protocols and practice

What is forward secrecy? If a long-term key leaks later, past recorded sessions stay safe. It is achieved by using ephemeral key exchange (ephemeral Diffie-Hellman or ECDH) for each session and deleting the ephemeral secrets afterwards. TLS 1.3 requires it.

Python Forward secrecy with and without ephemeral keys

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second half of the demo is a simplified stand-in: it shows that a leaked long-term key is useless against session keys that were never derived from it. The honest statement is that the recorded transcript holds only public values, and turning them into the session key means solving the Diffie-Hellman problem.

How does a certificate help? It binds a public key to an identity, signed by a certificate authority that your system already trusts. That is what lets a client know the key in a handshake belongs to the real server and not a man in the middle (certificates and PKI).

What does TLS give you? Confidentiality, integrity and server authentication (optionally client authentication) for data in transit, using a handshake with key exchange and certificates, then fast authenticated symmetric encryption for the data (how TLS works).

What is authenticated encryption and why does it matter? It encrypts and authenticates in one step (AES-GCM, ChaCha20-Poly1305), so tampered ciphertext is rejected before any decryption result is used. Encryption without authentication allows bit-flipping and padding-oracle attacks (authenticated encryption).

How do quantum computers affect cryptography? Shor's algorithm would break RSA, Diffie-Hellman and elliptic curves; Grover's algorithm only halves the effective strength of symmetric keys and hashes, so 256-bit keys remain comfortable. Migration to post-quantum schemes such as ML-KEM is underway (post-quantum cryptography).

Name five common implementation mistakes. Home-made crypto, ECB mode, nonce reuse, fast hashes for passwords, and keys in source code. See common cryptography mistakes.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A candidate says: 'We hash the message and send the hash to prove nobody changed it.' What is missing?

    A plain hash has no secret. Only a keyed construction (MAC) or a signature ties the digest to someone who holds a key.
  2. 2. What is the main benefit of ephemeral Diffie-Hellman in TLS?

    Session keys come from one-time secrets that are deleted, so recorded traffic cannot be decrypted with a later-stolen long-term key.
  3. 3. Why are real systems hybrid (public-key plus symmetric)?

    Hybrid designs combine the key-distribution strength of public-key cryptography with the speed of symmetric ciphers.
  4. 4. What does a salt NOT do?

    A salt prevents table reuse and duplicate detection, but a guessable password can still be found by guessing it against that user's salted hash.
  5. 5. Roughly how many tries does it take to find some collision in a 256-bit hash by brute force?

    The birthday bound gives about 2^(n/2) tries, so 2^128 for a 256-bit hash, which is out of reach.

Summary

  • Answer in four steps: the goal, the primitive, why alternatives fail, one pitfall.
  • Hash = no key; MAC = shared key; signature = private key with public verification.
  • Store passwords with Argon2id, scrypt, bcrypt or PBKDF2 and a unique salt; salts stop table reuse but do not rescue weak passwords.
  • RSA needs OAEP/PSS; ECC offers the same security with much smaller keys; Diffie-Hellman gives key agreement but needs authentication.
  • Forward secrecy comes from ephemeral key exchange; TLS 1.3 requires it.
  • Hybrid encryption, authenticated encryption and secure randomness are the defaults to name.

Next: Cryptography Cheat Sheet.