Find a course

Module 5: Cryptography in PracticeLesson 11 of 1115 min

Common Mistakes, Randomness and Post-Quantum Cryptography

Learn why you shouldn't roll your own crypto, how bad randomness and key handling break strong algorithms, and what quantum computers mean for encryption.

Step 1 of 14Learn

Strong math, weak systems

AES has never been practically broken. Neither has SHA-256, nor properly sized RSA or elliptic curve cryptography. Yet systems using these algorithms are compromised all the time. The cryptographer Bruce Schneier has long made the point that attackers go around cryptography rather than through it.

The weak points are almost always in how cryptography is used:

  • Weak keys and bad randomness: keys that are predictable, derived from weak passwords, or generated with a flawed random number generator.
  • Misuse of primitives: ECB mode, reused nonces, unauthenticated encryption, MD5 signatures, textbook RSA.
  • Key management failures: private keys committed to public code repositories, hard-coded in apps, left on unsecured servers, never rotated or revoked.
  • Side channels: leaking secrets through timing, power use, electromagnetic emissions or error messages.
  • Protocol mistakes: skipping certificate validation, accepting downgrades to old versions, comparing MACs non-constant-time.
  • The humans: phishing, coerced passwords, and users clicking through warnings.

This is the reason behind the most repeated advice in the field: don't roll your own crypto. That doesn't only mean don't invent algorithms. It also means don't assemble standard primitives into your own protocol when a vetted one exists. Use TLS for connections, a well-reviewed library such as libsodium or your platform's crypto API for encryption, and Argon2 or bcrypt for passwords, with their recommended defaults.

Step 2 of 14Practice · Diagram

Where real systems break

The algorithm at the bottom is rarely the weak point. Tap each layer.

Which layer has AES-256 sitting in it, the one that is almost never the cause of a breach?

Tap the shape that answers the question, then press Check.

Where real systems breakLayer diagram: People and processes, Protocol and usage, Implementation, Algorithm (AES, SHA-256).People and processesProtocol and usageImplementationAlgorithm (AES, SHA-256)
People and processes

Phishing, leaked keys in code repositories, skipped updates.

Protocol and usage

Nonce reuse, unauthenticated encryption, ECB mode.

Implementation

Bugs like Heartbleed, weak randomness, timing leaks.

Algorithm (AES, SHA-256)

Mathematically solid; almost never the cause.

Step 3 of 14Practice · Sort into groups

Sort each real-world failure into its root cause.

Tap an item, then tap its group (or drag it there).

Step 4 of 14Learn

Randomness is a security requirement

Keys, nonces, IVs and salts must be unpredictable. A 256-bit key generated from a predictable source might have only a few thousand realistic possibilities, regardless of its length.

Ordinary programming functions like JavaScript's Math.random() or Python's random module are pseudorandom number generators designed for simulations and games. They're fast and statistically even, but if an attacker sees some outputs, they can often predict the rest. They must never be used for security.

Security needs a cryptographically secure pseudorandom number generator (CSPRNG), seeded from real-world unpredictability (hardware noise, interrupt timings) that the operating system collects. Use:

  • crypto.getRandomValues() or crypto.randomUUID() in browsers and Node.js,
  • Python's secrets module,
  • the OS interfaces such as /dev/urandom or getrandom() on Linux, and the system random APIs on Windows and macOS.

History has harsh examples. In 2008, a Debian Linux packaging change accidentally removed most of the randomness from OpenSSL's key generation, leaving only about 32,768 possible keys per key type and architecture; for nearly two years, SSH and TLS keys made on affected systems could be guessed. In 2012, researchers scanning the internet found that tens of thousands of devices shared prime factors in their RSA keys because they generated keys at first boot before collecting enough randomness, letting the researchers factor those keys instantly.

Step 5 of 14Practice · Choose one

A developer needs to generate password-reset tokens in Python. Which approach is correct?

Step 6 of 14Learn

Side channels and constant time

A side-channel attack extracts secrets from how an implementation behaves physically rather than from its mathematical output.

  • Timing: if comparing a submitted MAC with the correct one stops at the first wrong byte, a correct first byte takes slightly longer to reject. By measuring response times over many attempts, an attacker can recover a valid tag byte by byte. The fix is constant-time code whose running time doesn't depend on secret values.
  • Cache timing: early software AES implementations used lookup tables whose memory access patterns depended on the key. Another program on the same machine could detect which table entries were cached, leaking key bits. Hardware AES instructions avoid this.
  • Power and electromagnetic analysis: measuring a smart card's power draw while it decrypts can reveal the key; tamper-resistant chips add countermeasures.
  • Error messages: padding oracle attacks exploit servers that respond differently to different decryption failures.
  • Speculative execution: the Spectre and Meltdown vulnerabilities, disclosed in 2018, showed that processors' performance tricks could leak data across security boundaries through cache timing.

Side channels are a major reason to rely on mature libraries. Their authors have spent years writing constant-time code and defending against these leaks, something a new implementation rarely gets right.

Step 7 of 14Tap to reveal

Three side channels

  1. Timing

    A comparison that stops at the first wrong byte leaks how many bytes were right. Fix: constant-time comparison.

  2. Power and electromagnetic

    A smart card's power draw changes with the bits it processes. Fix: masking and hardware shielding.

  3. Cache

    Code sharing a CPU can measure which memory a victim touched, as in Spectre-style attacks. Fix: table-free implementations and isolation.

Step 8 of 14Learn

Quantum computers and post-quantum cryptography

A large, error-corrected quantum computer would change cryptography in two very different ways.

Shor's algorithm (1994) could factor large numbers and compute discrete logarithms efficiently. That would break RSA, classic Diffie-Hellman and elliptic curve cryptography entirely: no realistic key size would be safe. These algorithms protect nearly all key exchange and signatures on the internet today.

Grover's algorithm speeds up brute-force search, roughly taking the square root of the work. Against AES-128, it would reduce the effective strength toward 264 in theory, though running it at that scale is far harder than the numbers suggest. The response is simple: use 256-bit symmetric keys, which remain strong. Hash functions are similarly affected only modestly.

No quantum computer today comes close to breaking RSA-2048; that would need millions of high-quality physical qubits. But there's an urgent reason to act anyway: harvest now, decrypt later. An adversary can record encrypted traffic today and decrypt it when quantum computers arrive. Secrets that must stay confidential for decades are already at risk.

So cryptographers designed post-quantum cryptography (PQC): new public-key algorithms based on math problems believed hard even for quantum computers, running on ordinary hardware. After an open competition that began in 2016, NIST published its first standards in August 2024:

  • ML-KEM (FIPS 203, from the Kyber design) for key exchange,
  • ML-DSA (FIPS 204, from Dilithium) and SLH-DSA (FIPS 205, from SPHINCS+) for signatures.

Major browsers and messaging apps have already deployed hybrid key exchange that combines X25519 with ML-KEM, so a connection stays secure as long as either algorithm holds.

Step 9 of 14Practice · Chart

Security left against a large quantum computer

Grover halves the effective strength of symmetric keys; Shor breaks RSA and elliptic curves completely.

Which algorithm keeps 128-bit security even against Grover's algorithm?

Tap the bar that answers the question, then press Check.

Security left against a large quantum computerBar chart with 5 categories: AES-128: Today 128 bits, With quantum attack 64 bits; AES-256: Today 256 bits, With quantum attack 128 bits; SHA-256 (preimage): Today 256 bits, With quantum attack 128 bits; RSA-3072: Today 128 bits, With quantum attack 0 bits; ECC P-256: Today 128 bits, With quantum attack 0 bits.0 bits100 bits200 bits300 bitsAES-128AES-256SHA-256(preimage)RSA-3072ECC P-256128 bits64 bits256 bits128 bits256 bits128 bits128 bits0 bits128 bits0 bits
Approximate effective security levels; no such quantum computer exists yet.
Security left against a large quantum computer
ItemTodayWith quantum attack
AES-128128 bits64 bits
AES-256256 bits128 bits
SHA-256 (preimage)256 bits128 bits
RSA-3072128 bits0 bits
ECC P-256128 bits0 bits

Step 10 of 14Practice · Sort into groups

Sort each algorithm by how a large quantum computer would affect it.

Tap an item, then tap its group (or drag it there).

Step 11 of 14Note

Step 12 of 14Timeline

Retired algorithms and their replacements

  1. 1998
    DES brute-forced

    Replaced by AES (2001).

  2. 2015
    RC4 banned in TLS

    Statistical biases made it unsafe.

  3. 2017
    SHA-1 collision

    Browsers stopped trusting SHA-1 certificates.

  4. 2024
    First post-quantum standards

    NIST published ML-KEM and ML-DSA.

Step 13 of 14Practice · What would you do?

What would you do?

A health records company stores patient data that must remain confidential for at least 30 years. Its data in transit is protected by TLS using classic X25519 key exchange, and records at rest use AES-256-GCM. What should it prioritize in light of quantum computing?

Step 14 of 14Practice · Choose one

Which statement best summarizes the advice behind 'don't roll your own crypto'?