Two parties derive a shared secret over a public channel — without ever transmitting it.
🌐
Step 1 — Choose Public Parameters
A large prime p and a primitive root g are published openly. Everyone — including attackers — can see these.
These values are not secret. p and g are the "rules of the game" agreed on publicly. Security comes from what each party does with their own private number, not from hiding p or g.
p (public prime)
—
g (generator)
—
g < p ?
—
🔐
Step 2 — Each Party Picks a Private Key
Alice picks secret 'a', Bob picks secret 'b'. Neither shares this number with anyone, ever.
Private keys are random integers, ideally in the range [2, p−2]. The larger the better. Neither party ever transmits these — they're used only locally to compute public values.
📡
Step 3 — Exchange Public Values
Each party computes a public value (A or B) and sends it over the open channel. An eavesdropper sees A and B but cannot recover a or b.
Alice sends A = gᵃ mod p | Bob sends B = gᵇ mod p These are one-way: computing A from a is easy, but reversing A → a requires solving the Discrete Logarithm Problem.
🙋 Alice
private a—
computes A = gᵃ mod p
public A—
✉ sends A publicly
→
A over public channel
←
B over public channel
🧔 Bob
private b—
computes B = gᵇ mod p
public B—
✉ sends B publicly
🤝
Step 4 — Derive the Shared Secret
Alice raises Bob's public value to her private key; Bob does the reverse. Both arrive at the same number — their shared secret.
Alice: S = Bᵃ mod p | Bob: S = Aᵇ mod p Both equal gᵃᵇ mod p by the laws of modular arithmetic. The shared secret S was never transmitted.
🙋 Alice computes
S = Bᵃ mod p
= —
Shared S—
🟰
🧔 Bob computes
S = Aᵇ mod p
= —
Shared S—
🔒 Shared Secret (never transmitted)
—
= gab mod p — both parties computed this independently
🛡️
Step 5 — Security: The Discrete Logarithm Problem
An eavesdropper sees p, g, A, and B — but cannot recover the shared secret without solving a problem that has no known efficient solution.
The Discrete Log Problem (DLP): given g, A, and p, find a such that gᵃ ≡ A (mod p). For large p (2048+ bits), no classical algorithm can do this efficiently.
What the attacker sees
What they need
Why it's hard
p, g, A=gᵃ mod p, B=gᵇ mod p
a or b
Discrete log — best known: sub-exponential, ~10³⁴ ops for 2048-bit
A and B
gᵃᵇ mod p directly
Computational DH problem — conjectured as hard as DLP
✓ DH provides forward secrecy
✓ RSA does not (old traffic can be decrypted if key is later compromised)
✓ TLS 1.3 mandates ephemeral DH (DHE/ECDHE)
Quantum threat: Shor's algorithm can also solve the DLP, breaking classical DH. This is why CRYSTALS-Kyber (a lattice-based KEM) was standardised by NIST in 2024 as the quantum-safe replacement.