Introduction

Almost every secret you send online today is protected by a bet: that factoring a huge number, or some cousin of that problem, is too slow for any attacker. It is a good bet. But it is still a bet — and a fast enough computer would win it.

In 1984, Charles Bennett and Gilles Brassard proposed a completely different idea. Instead of hiding a key behind a hard computation, BB84 lets two people — call them Alice and Bob — build a shared secret key out of single particles of light, where the secrecy is enforced by a law of nature: you cannot measure a quantum state without disturbing it.

The consequence is startling. Any eavesdropper who tries to read the key on its way across necessarily leaves errors behind. Alice and Bob just compare a few bits, see the damage, and throw the key away. Security stops being a guess about an algorithm and becomes a measurement.

Try It

Alice sends a stream of qubits, each polarized in one of two random bases (rectilinear + or diagonal ×). Bob measures each one in a basis he also picks at random. Whenever their bases happen to match, they keep the bit — that becomes the shared key.

<p class="hint">Alice sends qubits in random bases (+ or ×). Bob measures in his own random bases. They keep a bit only when the bases match. With <b>Eve</b> on, she must guess a basis before measuring — each wrong guess scrambles the qubit, so the kept bits start to disagree. Watch the error rate expose the spy.</p>
<div class="controls">
  <label><input type="checkbox" id="eve"> Eve is intercepting</label>
  <label>Qubits: <input type="range" id="n" min="8" max="40" value="20"></label>
</div>
<div class="btns">
  <button id="send" type="button">Send qubits</button>
  <button id="reset" type="button" class="ghost">Clear</button>
</div>
<div id="stream" class="stream"></div>
<div class="status" id="status">Press “Send qubits”.</div>
* { box-sizing: border-box; }
body { font-family: system-ui, sans-serif; color: #222; margin: 0; }
.hint { font-size: .9rem; color: #444; margin: 0 0 .7rem; line-height: 1.45; }
.controls { display: flex; gap: 1.2rem; flex-wrap: wrap; align-items: center;
            font-size: .9rem; margin: .3rem 0 .6rem; }
.controls input[type=range] { vertical-align: middle; }
.btns { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: .6rem; }
button { font: 600 14px system-ui, sans-serif; padding: .45rem .9rem; border: 1px solid #1d3557;
         background: #1d3557; color: #fff; border-radius: 8px; cursor: pointer; }
button.ghost { background: #fff; color: #1d3557; }
.stream { display: flex; flex-wrap: wrap; gap: 4px; margin: .4rem 0; }
.q { width: 30px; text-align: center; font: 700 13px ui-monospace, monospace;
     border-radius: 6px; padding: 4px 0; border: 1px solid #cdd9e3; background: #e8eef3; color: #1d3557; }
.q.drop { opacity: .35; }
.q.keep { background: #d4ead9; border-color: #9fcead; }
.q.err { background: #f6cdd2; border-color: #e29aa3; color: #9b1c2a; }
.status { font-size: 1rem; font-weight: 600; margin: .5rem 0; min-height: 1.4em; }
.status.ok { color: #0a7d33; }
.status.bad { color: #c92f3c; }
const fmt = (s, v) => s.replace(/%(\w+)%/g, (_, k) => String(v[k] ?? ''));
// BB84 simulation. A qubit carries a bit value encoded in one of two bases:
//   basis 0 = rectilinear (+):  bit0 = | (0deg),  bit1 = - (90deg)
//   basis 1 = diagonal   (×):  bit0 = / (45deg), bit1 = \ (135deg)
// Measuring in the SAME basis Alice used returns her bit exactly.
// Measuring in the WRONG basis returns a uniformly random bit (quantum collapse).
// Eve must guess a basis before measuring; a wrong guess collapses the state
// and re-sends a qubit that no longer matches Alice's. The sifted key (bases
// matched) then shows ~25% errors — the eavesdropper's signature.
const rnd = () => Math.random() < 0.5 ? 0 : 1;

// Measure a qubit prepared as (aBit in aBasis) using mBasis. Returns the bit read.
function measure(aBit, aBasis, mBasis) {
  return (mBasis === aBasis) ? aBit : rnd(); // wrong basis -> random outcome
}

function run() {
  const eve = document.getElementById('eve').checked;
  const N = +document.getElementById('n').value;
  const stream = document.getElementById('stream'); stream.innerHTML = '';
  let kept = 0, errors = 0;
  const SYM = [['|','-'],['/','\\']]; // [basis][bit]

  for (let i = 0; i < N; i++) {
    const aBit = rnd(), aBasis = rnd();      // Alice prepares
    let bit = aBit, basis = aBasis;          // state travelling to Bob

    if (eve) {                                // Eve intercepts and resends
      const eBasis = rnd();
      const eBit = measure(aBit, aBasis, eBasis);
      bit = eBit; basis = eBasis;            // Eve re-emits what she measured
    }

    const bBasis = rnd();                     // Bob's random basis
    const bBit = measure(bit, basis, bBasis);

    const div = document.createElement('div');
    div.className = 'q';
    if (bBasis === aBasis) {                  // sifting: bases match -> keep
      kept++;
      const wrong = bBit !== aBit;
      if (wrong) errors++;
      div.className += wrong ? ' keep err' : ' keep';
      div.textContent = SYM[aBasis][aBit];
    } else {
      div.className += ' drop';               // bases differ -> discard
      div.textContent = SYM[aBasis][aBit];
    }
    stream.appendChild(div);
  }

  const rate = kept ? Math.round(100 * errors / kept) : 0;
  const s = document.getElementById('status');
  if (!eve) {
    s.textContent = fmt('No spy. Kept %kept% bits, %errors% errors (%rate%%). Key is clean.', { kept: kept, errors: errors, rate: rate });
    s.className = 'status ok';
  } else if (rate >= 10) {
    s.textContent = fmt('Eavesdropper detected! %errors%/%kept% kept bits flipped (%rate%%). Discard the key.', { errors: errors, kept: kept, rate: rate });
    s.className = 'status bad';
  } else {
    s.textContent = fmt('Eve present but few errors this run (%rate%%) — send more qubits to expose her.', { rate: rate });
    s.className = 'status bad';
  }
  console.log('eve=' + eve, 'kept=' + kept, 'errors=' + errors, 'rate=' + rate + '%');
}

function reset() {
  document.getElementById('stream').innerHTML = '';
  const s = document.getElementById('status');
  s.textContent = 'Press “Send qubits”.'; s.className = 'status';
}
document.getElementById('send').addEventListener('click', run);
document.getElementById('reset').addEventListener('click', reset);
Result
Console

Run it once with no spy: the bits Alice and Bob keep agree perfectly. Now flip on Eve. Because she has to guess each basis before measuring, she gets it wrong half the time, and every wrong guess randomizes the qubit. The result is unmistakable: about 25% of the shared bits flip. That error rate is the eavesdropper's fingerprint — there is no way to listen without leaving it.

The Real Complexity

What makes BB84 different is where its security comes from.

  • Classical cryptography is computational. RSA and its relatives are safe only as long as factoring (or a related problem) stays hard. Nobody has proven it is — that safety leans on the open P vs NP question and would collapse against a large quantum computer running Shor's algorithm.
  • BB84 is information-theoretic. Its security does not assume the attacker is slow. It assumes only that quantum mechanics is correct — specifically the no-cloning theorem: an unknown quantum state cannot be copied. Eve cannot keep a perfect copy to measure later; she must disturb the original now.
  • It is proven, not conjectured. Unlike most of cryptography, BB84 has a rigorous security proof — first by Dominic Mayers (2001), with an elegant version by Peter Shor and John Preskill (2000). Given an honest error budget, the probability that an eavesdropper learns the key can be driven arbitrarily close to zero.

So BB84 sidesteps the entire complexity-theory gamble. There is no hard problem to solve faster — there is a physical fact to violate, and you cannot.

Where It Matters

"A key that a future computer can't break" is not a thought experiment — BB84 and its descendants are already running:

  • Metro fiber links: banks and data centers exchange QKD keys over dedicated optical fiber for high-value traffic.
  • Satellite QKD: China's Micius satellite distributed entangled and BB84-style keys between ground stations thousands of kilometers apart.
  • Government and defense networks: long-term secrets that must stay safe for decades cannot rely on math that a quantum computer might one day undo.
  • Hybrid post-quantum systems: QKD is often paired with classical algorithms to hedge against both today's and tomorrow's attackers.

The same threat that motivates BB84 — a quantum computer breaking classical schemes via Shor's algorithm — is what makes physics-based keys worth the hardware.

Conclusion

BB84 carries a quietly radical message: secrecy does not have to rest on a problem being hard. It can rest on the universe refusing to be copied. Alice and Bob never need to assume their adversary is slow — they only need to count a few errors and trust that measurement disturbs.

So while the rest of cryptography watches nervously for the day a quantum computer cracks Shor's algorithm against it, BB84 simply shrugs. Its security was never a wager on P vs NP. It was a wager on physics — and that is one bet we have never seen the universe lose.

Share this article

Pick a channel — or use your device's native share sheet.

Comments

Loading comments...

https://www.kipuhub.com/en/article/bb84/Content licensed under CC BY-NC 4.0.