liveBook · Manning

Diffie–Hellman key exchange

As explained in Hacking Cryptography

Diffie–Hellman key exchange is a key-agreement protocol in which participants derive a shared secret from exchanged public values and retained private exponents.

Diffie–Hellman key exchange lets two participants derive the same secret while sharing only public values. Each chooses a private exponent, publishes its modular exponentiation, and combines the received value with its own private exponent. section 8.3.1

The exchange proceeds as follows:

  • Agree on a prime p and generator g. section 8.3.1
  • Choose private exponents a and b, then publish A = gᵃ mod p and B = gᵇ mod p. section 8.3.1
  • Compute S = Aᵇ mod p = Bᵃ mod p = gᵃᵇ mod p. section 8.3.1
  • Rely on the difficulty of recovering a private exponent from a public value: the discrete logarithm problem. section 8.3.1

A small modular example

Using p = 13 and g = 7, Alice chooses a = 3 and publishes A = 5. Bob chooses b = 9 and publishes B = 8. Alice computes 8³ mod 13 = 5; Bob computes 5⁹ mod 13 = 5. Both obtain the shared secret S = 5, while an eavesdropper sees only the public values. section 8.3.1

Table 8.1 Exponentiation table for multiplicative group modulo 13
Base \ Exponent (n) 0 1 2 3 4 5 6 7 8 9 10 11 12
\(1^n \bmod 13\) 1 1 1 1 1 1 1 1 1 1 1 1 1
\(2^n \bmod 13\) 1 2 4 8 3 6 12 11 9 5 10 7 1
\(3^n \bmod 13\) 1 3 9 1 3 9 1 3 9 1 3 9 1
\(4^n \bmod 13\) 1 4 3 12 9 10 1 4 3 12 9 10 1
\(5^n \bmod 13\) 1 5 12 8 1 5 12 8 1 5 12 8 1
\(6^n \bmod 13\) 1 6 10 8 9 2 12 7 3 5 4 11 1
\(7^n \bmod 13\) 1 7 10 5 9 11 12 6 3 8 4 2 1
\(8^n \bmod 13\) 1 8 12 5 1 8 12 5 1 8 12 5 1
\(9^n \bmod 13\) 1 9 3 1 9 3 1 9 3 1 9 3 1
\(10^n \bmod 13\) 1 10 9 12 3 4 1 10 9 12 3 4 1
\(11^n \bmod 13\) 1 11 4 5 3 7 12 2 9 8 6 10 1
\(12^n \bmod 13\) 1 12 1 12 1 12 1 12 1 12 1 12 1

Use the exponentiation table to verify the public values and shared-secret calculations modulo 13. section 8.3.1

How Hacking Cryptography covers it

How deep it goes
Derives public values and shared-secret equations through modular arithmetic, without implementation code or parameter-selection guidance.
Examples
Works through Alice and Bob using p = 13, g = 7, private exponents 3 and 9, and shared secret 5.
What it assumes
Teaches the discrete logarithm problem and modular-exponentiation notation before deriving the exchange.
What you can do after
Evaluate how public values produce a shared secret without exposing either private exponent.
How it connects
Connects discrete logarithms, modular arithmetic, public-key cryptography, TLS, and ElGamal encryption.

Also explained in

Compare how 4 books explain Diffie–Hellman key exchange →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.