Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 39 ยท Applications and Outlook

Quantum Cryptography: BB84 Key Distribution

Learn quantum key distribution and the BB84 protocol: how measurement disturbance reveals eavesdroppers, with a NumPy simulation and a post-quantum overview.

Two ideas connect quantum computing and security. The first is a threat: Shor's algorithm could break RSA and elliptic-curve cryptography. The second is a defense that uses quantum physics itself: quantum key distribution (QKD), which lets two parties create a shared secret key such that any eavesdropper unavoidably leaves a trace. This lesson explains the best-known protocol, BB84 (Bennett and Brassard, 1984), simulates it, and places QKD next to its main rival for protecting the future internet, post-quantum cryptography.

The problem QKD solves

Symmetric encryption with a random key as long as the message (the one-time pad) is perfectly secret, provided the key is truly random, used once and shared safely. The hard part is sharing the key. Public-key cryptography solves it with math assumptions (factoring, discrete logarithms) that a large quantum computer would break. QKD instead bases the key exchange on two physical facts:

  • No-cloning. An unknown quantum state cannot be copied, so an eavesdropper (Eve) cannot keep a perfect copy while forwarding the original.
  • Measurement disturbs. Measuring in the wrong basis randomizes the result and changes the state.

BB84, step by step

Alice sends qubits to Bob over a quantum channel (for example photons in a fiber). They also share an authenticated classical channel, which Eve may listen to but not alter.

  1. Alice picks random bits and, for each, a random basis: Z (|0>, |1>) or X (|+>, |->). She sends the matching qubit.
  2. Bob measures each qubit in a randomly chosen basis, independently of Alice.
  3. Sifting. Over the classical channel, they announce their bases (not bits) and keep only positions where the bases matched. That is about half of the qubits. With no noise and no Eve, those bits agree exactly.
  4. Error estimation. They publicly compare a random sample of the sifted bits. The quantum bit error rate (QBER) should be near zero for an honest channel.
  5. Post-processing. If the QBER is low enough (the usual limit is about 11% for the basic protocol), they run error correction to fix remaining differences and privacy amplification (hashing to a shorter key) to remove whatever Eve may know.
sequenceDiagram
    participant A as Alice
    participant E as Eve (optional)
    participant B as Bob
    A->>B: Qubits in random bases (Z or X)
    Note over E: Intercept-resend would disturb states
    B->>B: Measure in random bases
    A->>B: Announce bases (classical channel)
    B->>A: Announce bases
    A->>B: Compare a sample to estimate the error rate
    A->>B: Error correction + privacy amplification

BB84: quantum transmission, then public discussion that reveals only bases and a sample.

What Eve's attack does

Suppose Eve intercepts every qubit, measures it in a random basis and sends a new qubit in the state she found (intercept-resend). When she picks the wrong basis (50% of the time), her resent qubit has a random value in the true basis, so Bob gets the wrong bit half of the time. The net effect on the sifted key: 50% x 50% = 25% errors. Any such attack raises the QBER, so a visible error rate means someone is listening (or the channel is bad; QKD cannot tell which, and treats both as insecurity).

Simulate BB84

Each qubit below is an actual 2-element state vector, and each measurement projects onto the chosen basis.

Python BB84 with and without an eavesdropper

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.

Without Eve the QBER is exactly 0 and about half the qubits survive sifting. With intercept-resend it is close to 25%, as predicted.

How many bits must be compared?

If Eve attacks every qubit, each sampled sifted bit has a 25% chance of being wrong, so after comparing k bits the probability that she goes unnoticed is (3/4)^k. With k = 100, that is about 3 x 10^-13. If she attacks only a fraction of qubits she learns less but also leaves smaller traces; the security proofs handle every strategy by bounding Eve's information as a function of the QBER.

Finally, privacy amplification: a random hash function compresses the key to a shorter one about which Eve has negligible information. A simple universal hash multiplies the key by a random binary matrix modulo 2.

Python Detecting Eve and compressing the key by hashing

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 code does not prove information-theoretic security, but it shows the structure: the final key is shorter, and a wrong guess about the hidden part gives an unrelated key.

Beyond BB84

  • E91 / BBM92. Use entangled pairs and test Bell inequalities; correlations of the shared qubits must violate the inequality for the key to be secure (the entanglement chapter shows how such correlations arise).
  • Decoy-state BB84. Real lasers send weak pulses that sometimes contain several photons, which Eve can exploit (the photon-number-splitting attack). Decoy pulses of different intensities detect this.
  • Measurement-device-independent (MDI) and twin-field QKD. Remove or relax trust in the detectors and extend distance.
  • Satellite QKD. Because fiber loss grows exponentially with distance, satellites (the Chinese Micius mission, European and other programs) link far-apart ground stations, and trusted nodes or future quantum repeaters extend networks.

Limits of QKD

  • It distributes keys, not messages, and needs an authenticated classical channel (usually using a pre-shared secret or a post-quantum signature).
  • Rates fall quickly with distance (fiber works for roughly 100 to 400 km without repeaters).
  • Security rests on perfect implementation; real attacks target detectors (blinding attacks) and other hardware flaws, not the physics.
  • It needs special hardware and point-to-point links, so national security agencies (NSA, NCSC, ANSSI and others) generally recommend post-quantum cryptography for mainstream use and QKD only for niche applications.

Post-quantum cryptography

Post-quantum cryptography (PQC) uses ordinary software and hardware but replaces RSA/ECC with problems believed to be hard even for quantum computers. In 2024, NIST published the first standards:

  • ML-KEM (from CRYSTALS-Kyber): key encapsulation based on lattices (module learning with errors).
  • ML-DSA (from CRYSTALS-Dilithium) and SLH-DSA (from SPHINCS+, hash-based): digital signatures.
  • More algorithms (such as the code-based HQC and the FN-DSA signature from Falcon) were selected or are in standardization.

Migration is under way: browsers and TLS libraries already offer hybrid key exchange (classical plus ML-KEM), and organizations are inventorying where RSA and ECC are used. The deadline pressure comes from "harvest now, decrypt later": encrypted data recorded today may be decrypted by a future quantum computer, so long-lived secrets need protection now.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In BB84, what do Alice and Bob announce publicly during sifting?

    They reveal only which basis each used and keep positions where the bases matched.
  2. 2. What QBER do you expect from an intercept-resend attack on every qubit?

    Eve picks the wrong basis half the time, and a wrong basis gives Bob a wrong bit half the time.
  3. 3. Which physical principle forbids Eve from copying qubits and keeping the originals intact?

    Unknown quantum states cannot be copied exactly.
  4. 4. Why do most agencies recommend post-quantum cryptography for general use rather than QKD?

    QKD has distance, hardware and authentication limits; PQC works over the existing internet.
  5. 5. What is 'harvest now, decrypt later'?

    It is the reason long-lived data needs post-quantum protection already.

Summary

  • QKD uses no-cloning and measurement disturbance so that eavesdropping shows up as errors.
  • BB84: random bits and bases, sifting, error estimation, error correction and privacy amplification.
  • An intercept-resend attack causes about a 25% error rate in the sifted key; checking k bits misses it with probability (3/4)^k.
  • QKD has real-world limits; post-quantum cryptography (ML-KEM, ML-DSA, SLH-DSA) is the main practical defense against future quantum attacks.

Next: The Outlook for Quantum Computing