π BB84 Quantum Key Distribution: Securing the Future with Qubits

"In the quantum world, even eavesdropping leaves fingerprints."
β The promise of quantum cryptography
π§ Introduction
As cyberattacks grow more advanced, the need for unbreakable encryption becomes critical. Enter quantum cryptography β a field that uses the laws of quantum mechanics to secure communication. At the heart of this revolution lies the BB84 algorithm, the first and most famous Quantum Key Distribution (QKD) protocol.
In this blog, we'll demystify BB84, simulate it using Python (with Qiskit Aer), and show how two parties β Alice and Bob β can exchange a secret key that no hacker can steal undetected.
π§© What is BB84?
Developed in 1984 by Charles Bennett and Gilles Brassard, BB84 is a protocol that lets two people (traditionally Alice and Bob) generate a shared, secret key using quantum bits β or qubits.
Its magic lies in quantum rules like:
You can't clone a qubit (no-cloning theorem).
You disturb a qubit when you try to observe it.
Measurement outcomes depend on the basis you choose.
This means that if an attacker (Eve) tries to spy on the communication, her interference can be detected.
π How BB84 Works (Step-by-Step)
Letβs break down the process:
1. Alice generates random bits and bases
Bits: 0s and 1s
Bases: Z (rectilinear) or X (diagonal)
Bit | Basis | Qubit sent |
0 | Z | ` |
1 | X | ` |
2. Alice sends qubits to Bob
- She encodes each bit using the basis and sends it over a quantum channel.
3. Bob randomly chooses bases to measure
If his basis matches Aliceβs, he gets the correct bit.
If not, his result is random.
4. Alice and Bob compare their bases
Over a public channel, they reveal only the bases β not the bits.
They keep only the bits where their bases matched.
5. They now share a secret key
This shared bitstring is their quantum key.
If an eavesdropper (Eve) tried to listen in, her measurements would introduce detectable errors.
π§ BB84 Simulation Using Qiskit
We use Qiskit Aer, a local simulator, so you donβt need a paid Quantum Simulator.
π§ͺ Installation
install qiskit matplotlib
π Folder Structure
CopyEditQuantumBB84/
βββ bb84_simulation.py
π§Ύ Core Python Code
Hereβs a simplified version:
from qiskit import QuantumCircuit, Aer, execute
import random
def generate_bits_bases(n):
bits = [random.randint(0, 1) for _ in range(n)]
bases = [random.choice(['Z', 'X']) for _ in range(n)]
return bits, bases
def create_qubits(bits, bases):
circuits = []
for bit, basis in zip(bits, bases):
qc = QuantumCircuit(1, 1)
if bit == 1:
qc.x(0)
if basis == 'X':
qc.h(0)
circuits.append(qc)
return circuits
def measure_qubits(circuits, bob_bases):
results = []
for qc, basis in zip(circuits, bob_bases):
if basis == 'X':
qc.h(0)
qc.measure(0, 0)
backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend, shots=1)
result = job.result()
measured_bit = int(list(result.get_counts().keys())[0])
results.append(measured_bit)
return results
def sift_keys(alice_bases, bob_bases, alice_bits, bob_results):
key_alice = []
key_bob = []
for a_base, b_base, a_bit, b_bit in zip(alice_bases, bob_bases, alice_bits, bob_results):
if a_base == b_base:
key_alice.append(a_bit)
key_bob.append(b_bit)
return key_alice, key_bob
def run_bb84(n=50):
alice_bits, alice_bases = generate_bits_bases(n)
bob_bases = [random.choice(['Z', 'X']) for _ in range(n)]
circuits = create_qubits(alice_bits, alice_bases)
bob_results = measure_qubits(circuits, bob_bases)
key_alice, key_bob = sift_keys(alice_bases, bob_bases, alice_bits, bob_results)
print("Alice's Key:", key_alice)
print("Bob's Key: ", key_bob)
print(f"Match Rate: {sum([a == b for a, b in zip(key_alice, key_bob)]) / len(key_alice) * 100:.2f}%")
if __name__ == "__main__":
run_bb84()
π Sample Output
pgsqlCopyEditAlice's Key: [1, 0, 1, 1, 0]
Bob's Key: [1, 0, 1, 0, 0]
Match Rate: 80.00%
π‘οΈ What Makes It Secure?
Quantum no-cloning theorem: Eve cannot copy qubits without disturbing them.
Basis mismatch = random result
Eavesdropping detection: Alice and Bob can detect Eve by checking a few key bits.
π Real-World Use Cases
Field | Application |
Banking | Secure key exchange |
Government | Quantum-safe communication |
Research Labs | Quantum VPNs and long-distance QKD |
π§© Final Thoughts
The BB84 protocol is more than just a theory β it's the foundation of secure communication in the post-quantum world. Whether you're exploring quantum computing or working in cybersecurity, understanding this protocol puts you ahead of the curve.
π βWith BB84, secrecy is not in the complexity of math β but in the laws of nature.β
Subscribe to my newsletter
Read articles from Jenish patel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
