Answers · The network and the web
How can two computers agree on a secret key when anyone can listen?
In shortEach computer sends a scrambled number made from a private number; the shared key is made by scrambling the other computer's number with one's own private number, and a listener cannot unscramble it.
Two computers agree a key by sending each other a scrambled value and never sending the private number that went into it. Each then combines the other side's scrambled value with its own private number, and both arrive at the same shared key that a listener recording every message cannot reconstruct.
The public starting values are a prime, called the modulus, and a base. Call the modulus and the base . One side chooses a private exponent , computes and sends it. The other chooses a private exponent , computes and sends it. The first side raises what it received to , the second raises what it received to , and both get the same number because the two exponentiations commute.
Worked with numbers small enough to check
The public values are and . The first side picks . It computes , and , so it sends . The second side picks . It computes , and , so it sends .
The first side receives and computes . By repeated squaring, , , and . The second side receives and computes . Here , , , so . Both hold .
| value | first side | second side | sent on the wire | listener |
|---|---|---|---|---|
| p = 23 | yes | yes | yes | yes |
| g = 5 | yes | yes | yes | yes |
| a = 6 | yes | no | no | no |
| b = 15 | no | yes | no | no |
| g^a = 8 | yes | yes | yes | yes |
| g^b = 19 | yes | yes | yes | yes |
| s = 2 | yes | yes | no | no |
The natural guess, and why it fails
The natural guess is that the listener, seeing and , can simply combine them. It cannot. Multiplying them gives , and is not the key. Raising to the th power is not the same as raising to the th power; only the second is what the second side did. To reach , the listener needs either or . Recovering from means trying exponents until one produces . With there are only nonzero exponents to try, and the listener can do that by hand. That is exactly the weakness the real sizes are chosen to remove: the same search is still the only known route, but the number of candidates is made far too large to run.
Recovering an exponent from is the discrete logarithm problem. No fast method is known at the sizes used, but nobody has proved that no fast method exists. The security is an assumption, not a theorem. If someone found a fast method, the listener could recover or from the published values and compute exactly as the two sides do. The method is used because it has survived sustained attack, not because it has been proved safe.
Where it stops being enough
Two conditions matter. The modulus must be large enough that the search is out of reach; a modulus that is too small makes the listener's job a finite computation, as it was with . Reusing one modulus across many conversations is also dangerous, because the expensive part of the attack depends on the modulus and not on the particular pair of private exponents, so an attacker can precompute once and reuse the result.
Separately, this exchange gives secrecy against a listener who only records. It gives nothing against an attacker who can alter messages, because that attacker can intercept both public values, substitute its own, and run two separate exchanges, one with each side. Both sides then share a key with the attacker rather than with each other, and the arithmetic is perfectly valid. Authentication, not more arithmetic, is what closes that gap.
Worth keeping
- Two parties can agree a shared key by publishing values derived from private exponents and combining the other published value with one's own exponent modulo a public prime.
- An eavesdropper who records the published values cannot compute the key without solving the discrete logarithm problem, for which no fast method is known.
- Seeing the public numbers is not enough to compute the key, because the private exponents never cross the wire.
Where this goes next
This question is one step of How a Message Stays Private, a course that works through its subject from the beginning. The page that teaches this part of it is Agreeing a Secret in Public, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- Why does the TLS handshake take so many round trips before any data is sent?It is not one key exchange. The handshake agrees a secret, proves the server's identity and checks for tampering, and modern TLS fits those into one round trip; a fresh connection still needs three before the page starts.
- If a website has HTTPS, can someone on the same Wi-Fi read my password?No. HTTPS encrypts the form contents and cookies, so someone on the same Wi-Fi cannot read the password itself; they can still see the site, timing and packet sizes.
- How does a man-in-the-middle attack still work if the key exchange is secure?A key exchange proves only that you share a secret with whoever answered. An attacker on the path answers you themselves and opens a second exchange to the real server, so both halves look secure.
- Does HTTPS hide the domain name of the site I'm visiting from my ISP?Usually not: the site's name is sent in readable form twice, once when your computer looks up its address and once when the secure connection opens.
- Where should a session ID be stored so JavaScript can't read it?Store it in an HttpOnly cookie set by the server; that is the only browser storage JavaScript running on the page cannot read.
- What does it mean for an API method to be safe?A safe method is one that changes nothing on the server when it is called, so a caller may repeat it freely and a cache or a crawler may use it without anything being altered.