Diffie-Hellman Key Exchange
How Ruby and Julia share a secret — even when someone is watching
Ruby and Julia each start with a common paint everyone can see. Then each secretly adds her own secret color — creating a unique public color.
The algorithm transports the public colors between them.
* Assuming paint separation is expensive!
Now each adds her secret color to the other person's public color.
Public color + my secret = shared secret!
Try it yourself
Colors on the left, numbers on the right — same idea, two languages
1 Agree on a public starting point
2 Each picks a secret
3 Combine public + secret = public value
Public values are exchanged openly
(assuming paint separation is expensive!)
4 Add your secret to the other's public value
They match!
Both arrive at the same secret — without ever sharing their private values.
Mixing paint is easy, but separating a mixed color back into its original paints is practically impossible. An eavesdropper sees the public colors but can't un-mix them.
Computing ga mod p is fast, but figuring out a from the result (the "discrete logarithm") is extremely hard for large numbers.
Both sides get the same answer because:
(ga)b mod p = (gb)a mod p = gab mod p