Walk through a concrete numerical example of the Diffie-Hellman key exchange.
With p=31 and generator g=3: Alice picks a=8, sends $3^8 \mod 31 = 20$. Bob picks b=6, sends $3^6 \mod 31 = 16$. Both compute $K = 4$.
* Worked example: secrets a=8 and b=6 both land on the shared key K = 4. *
The reason to work it with tiny numbers is to watch the two sides land on the same $K$ without it ever crossing the wire — and to feel why the security evaporates once $p$ is small: here Eve really could just try every exponent, which is exactly the work that becomes hopeless when $p$ has hundreds of digits.
Setup: $p = 31$, $g = 3$ (a generator of $\mathbb{Z}_{31}^*$)
Alice (secret a = 8):
- Computes $A = 3^8 \mod 31 = 6561 \mod 31 = 20$
- Sends $A = 20$ to Bob
Bob (secret b = 6):
- Computes $B = 3^6 \mod 31 = 729 \mod 31 = 16$
- Sends $B = 16$ to Alice
Shared key computation:
- Alice: $K = 16^8 \mod 31 = 4$
- Bob: $K = 20^6 \mod 31 = 4$
Both get $K = 4$ — the shared secret!
Eve knows: $p = 31$, $g = 3$, $A = 20$, $B = 16$. She'd need to solve $3^a \equiv 20 \mod 31$ to find $a = 8$ — easy for small numbers, but infeasible when $p$ has 1000+ digits.
Go deeper:
Diffie-Hellman, the Mathematics bit — Computerphile — works the modular exponentiation by hand.
Diffie-Hellman key exchange — Wikipedia — includes its own worked numeric example.