πŸ” BB84 Quantum Key Distribution: Securing the Future with Qubits

Jenish patelJenish patel
4 min read

"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)

BitBasisQubit sent
0Z`
1X`

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

FieldApplication
BankingSecure key exchange
GovernmentQuantum-safe communication
Research LabsQuantum 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.”

0
Subscribe to my newsletter

Read articles from Jenish patel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jenish patel
Jenish patel