The BB84 Quantum Key Distribution Explorer

Alice and Bob want a shared secret key, and want to know if anyone listened in. Photons make that possible.

Alice makes choices. Alice sends Bob a stream of single photons. For each one she randomly and independently chooses a bit (0 or 1) and a basis in which to encode it. There are two bases: the rectilinear basis + (H and V polarisation) and the diagonal basis × (D and A, at ±45°).

Bob makes choices. Bob receives the stream of photons also picks a basis at random to measure each photon. If the chosen basis matches Alice's choice for that photon, he reads her bit perfectly. If not, the photon is equally likely to give 0 or 1, so his result is pure noise, exactly as a filter at 45° to a polarised photon passes it half the time (Malus's law, cos²45° = ½).

Encoding table

bit 0
bit 1
basis +
H (0°)
V (90°)
basis ×
D (45°)
A (135°)

Communicate choices to generate a key. Now, Alice and Bob communicate which choices they made (but not their measurement results). This allows them to isolate only the measurement outcomes where they made the same choice - therefore have the same result. This string of 1's and 0's forms a secret binary string that they both hold without requiring transmission on classical channels.

Checking for eavesdroppers. An eavesdropper, Eve, cannot copy an unknown quantum state (due to the no-cloning theorem), and she does not know which basis Alice used. If she intercepts the photon and measures it in the wrong basis, she scrambles the photon, and some of Bob's matching-basis results (which should have matched Alice's measurements) will then be wrong. If Alice and Bob take a small sample of their secret key transmit it publicly for comparison they can check for the introduced errors that would signal Eve's interference. On this page you will watch that happen and work out how many bits Alice and Bob must sacrifice to notice it.

Run the protocol

Send photons one at a time and observe how Alice and Bob's independent action results in valid or invalid candidate bits. Switch Eve on to see how an intercept-and-resend attack can start to introduce errors.

How much does Eve disturb the key?

Each trial, Alice and Bob end up with a sifted key and publicly compare some of its bits. Any mismatch exposes Eve. But if she only intercepts some photons, she may never touch the key at all. Run 100 independent trials to see which ones she snoops on, and which of those she is caught in.

Where 25% comes from. Eve guesses the wrong basis half the time. Then her resent photon is random in Alice's basis, so when Bob's basis matches Alice's he errs half the time. That gives ½ × ½ = 25% errors in the sifted key for each intercepted photon.

Eve intercepts
Sifted key length
Bits compared
Eve snooped, caught   Eve snooped, undetected   Eve never touched the key

Alice and Bob sacrifice the compared bits (they are now public), so there is a trade-off: comparing more bits makes detection near-certain but leaves a shorter key. With few bits Eve can easily slip through, so not seeing a mismatch is not proof she is absent. In practice they compare a large sample and measure the error rate (QBER); above roughly 11% the key cannot be made secure and they abort. Real systems also have noise and detector loss, so a small error rate does not by itself prove Eve is present.