Two parties agree on a shared secret over an open channel without ever sending it — security rests on the hardness of the discrete logarithm.
Diffie-Hellman lets two strangers agree on a shared secret key while everything they send is public. Nobody ever transmits the key itself — both sides *compute* the same number independently. An eavesdropper sees every message and still cannot reconstruct it. It's the move that bootstraps encrypted sessions across the open internet.
p and a generator g (both can be sent in the clear, or be well-known constants).a, computes A = g^a mod p, and sends A.b, computes B = g^b mod p, and sends B.s = B^a mod p. Bob computes s = A^b mod p.s = g^(ab) mod p — the shared secret, never transmitted.It works because exponentiation commutes: (g^a)^b = (g^b)^a = g^(ab) — all taken mod p. The attacker sees g, p, A, and B, but recovering a from A = g^a mod p is the discrete logarithm problem, which has no known efficient solution for large primes.
Public: p = 23, g = 5
Alice: a = 6 -> A = 5^6 mod 23 = 8
Bob: b = 15 -> B = 5^15 mod 23 = 19
Alice: s = B^a mod 23 = 19^6 mod 23 = 2
Bob: s = A^b mod 23 = 8^15 mod 23 = 2
Shared secret = 2 (never sent on the wire)