How Two Strangers Can Share a Secret in Public

2026-02-28 · cryptography

Alice and Bob have never met. They're talking on a phone line that Eve is tapping. Every word they say, Eve hears. Yet somehow, by the end of the conversation, Alice and Bob share a secret number — and Eve has no idea what it is.

This isn't magic. It's math. And it's one of the most elegant ideas in modern cryptography.

The Problem

Imagine you want to send a locked box to someone. You need to give them a key first. But if you send the key, anyone watching can copy it. Then they can open your box.

This was the fundamental problem of cryptography for thousands of years. Secure communication required a secure channel to exchange keys first. Which meant... you already needed security to have security.

The Paradox

To communicate securely, you need a shared secret.
To share a secret, you need to communicate securely.

In 1976, Whitfield Diffie and Martin Hellman published a paper that broke this loop. They showed that two people could create a shared secret over a public channel, even if someone was listening to everything they said.

The Mixing Bowl Analogy

Before we get to the math, let's use colors. Imagine mixing paint.

Interactive: Color Mixing
Alice's secret
+
Public base
=
Mixed (sent)

Simplification. Real math is one-way — easy to mix, nearly impossible to unmix.

Here's the key insight: mixing paint is easy. Unmixing it is nearly impossible.

Alice picks a secret color (blue). She mixes it with a public base color (yellow) everyone agrees on. She sends the result (green) to Bob over the public channel. Eve sees green, but can't figure out which shades of blue and yellow made it.

Bob does the same with his secret color (red). He mixes it with yellow, sends the result (orange) to Alice.

Now here's the magic: Alice takes Bob's orange and adds her blue. Bob takes Alice's green and adds his red. They both end up with the same color — but Eve, who only saw green and orange, can't recreate it.

The Math: Modular Exponentiation

Colors are nice, but computers need numbers. The mathematical equivalent of "easy to mix, hard to unmix" is modular exponentiation.

Here's the recipe:

  1. Everyone agrees on a public prime number p and a base g
  2. Alice picks a secret number a, computes A = g^a mod p, sends A to Bob
  3. Bob picks a secret number b, computes B = g^b mod p, sends B to Alice
  4. Alice computes s = B^a mod p
  5. Bob computes s = A^b mod p

They both get the same s. Why?

The Magic
Alice: s = B^a mod p = (g^b)^a mod p = g^(ba) mod p
Bob:   s = A^b mod p = (g^a)^b mod p = g^(ab) mod p

Since ba = ab: Alice and Bob get the same s!
          

The security comes from the discrete logarithm problem: given g^a mod p, it's computationally infeasible to find a. With large enough numbers (2048+ bits), this would take longer than the age of the universe.

A Small Example

Real cryptography uses huge numbers. But let's try a toy example so you can see it work.

Interactive Demo

Public parameters: p = 23, g = 5

Shared secret:

Change the secret numbers and watch the shared secret update.

Why Eve Can't Break This

Eve sees:

  • p = 23
  • g = 5
  • A = 8 (Alice's public value)
  • B = 19 (Bob's public value)

To find the secret, Eve needs either a or b. She needs to solve:

5^a ≡ 8 (mod 23)

In this tiny example, Eve could try all possibilities (brute force). But with a 2048-bit prime, there are more possibilities than atoms in the observable universe. The best known algorithms still take longer than the universe has existed.

The Legacy

Diffie-Hellman key exchange is used everywhere:

  • TLS/HTTPS — Every time you see the lock icon in your browser
  • SSH — When you connect to a server
  • Signal, WhatsApp — End-to-end encrypted messaging
  • VPN protocols — Secure tunnels

The next time you see that little lock icon, remember: two strangers just agreed on a secret, right in front of everyone's eyes.

The Key Insight

Some operations are easy in one direction
but nearly impossible to reverse.

This asymmetry is the foundation of modern cryptography.

Further reading: The original 1976 paper · Elliptic Curve Diffie-Hellman (modern variant) · Man-in-the-middle attacks (the catch)