Diffie-Hellman key exchange

The Diffie-Hellman key exchange algorithm is named after its two inventors, Whitfield Diffie and Martin Hellman. The system, which can be used to establish a (symmetric) key, relies on the difficulty of the so-called discrete logarithm problem for its security.

There are two system-wide parameters, a prime number p and a value g, with g < p, such that for any number from n ∈ {1,2,...,p - 1}, there is some k for which n = gk mod p. The number g is known as a "generator".

If our old friends Alice and Bob want to generate a shared secret key, they each generate a random secret value less than p - 1; call Alice's secret value a and Bob's secret value b. Then Alice computes a public value, ga mod p, which she sends to Bob, and Bob create his public value gb mod p, which he sends to Alice.

Alice then uses Bob's public value and her secret value to compute (gb)a mod p, while Bob computes (ga)b mod p. By elementary properties of exponents, these are equal, and this value is the shared secret.

Diffie-Hellman is subject to a man-in-the-middle (MiM) attack, as illustrated here.

To prevent the man-in-the-middle attack, your textbook suggests the following.