Quantum Cryptography Simulation in Python — ELI5

Imagine you want to send a secret note to your friend, but you’re worried someone is peeking at it along the way. Regular secret codes are like really complicated locks — hard to pick, but not impossible. A smart enough person with enough time could crack them.

Quantum cryptography is different. It uses a law of physics to make eavesdropping literally impossible without getting caught.

Here’s the trick: in quantum physics, looking at something changes it. If someone is a quantum particle and you peek at it, you leave fingerprints you can’t wipe off.

The most famous quantum cryptography method is called BB84, and it works like this:

  1. Alice (the sender) sends a stream of special light particles (photons) to Bob (the receiver). Each photon carries one bit of the secret key. But she randomly prepares each photon in one of two different ways.

  2. Bob randomly chooses how to measure each photon. Sometimes he guesses the right way, sometimes wrong.

  3. Alice and Bob compare their choices (but not the results!) over a regular phone call. They keep only the bits where Bob guessed the right measurement — these become their shared secret key.

  4. Here’s the magic: if Eve (an eavesdropper) tried to peek at the photons, she’d accidentally change some of them. Alice and Bob can detect this by comparing a few bits of their key. If too many don’t match, they know someone was listening and they throw it away.

It’s like sending a message written in disappearing ink that smudges if anyone else reads it. If the message arrives un-smudged, nobody peeked.

In Python, you can simulate this entire process — the photons, the measurements, even a simulated eavesdropper — and watch how the math proves the security works.

One thing to remember: Quantum cryptography doesn’t just make codes hard to break — it makes eavesdropping detectable by the laws of physics, which is a completely different kind of security.

pythonquantum-computingcryptographyquantum-key-distribution

See Also