Raising a number to a power, modulo n, is just walking around an n-hour clock. Watch which steps visit every hour, then see how Alice and Bob use that walk to agree on a secret in plain sight.
Explore · modular exponentiation
Pick a generator g and watch the hand step
g⁰ → g¹ → g² → … around the clock, each step multiplying by g (mod
n). A prime clock has generators that visit every hour;
a composite clock traps some steps in a small loop. Press
Race both to compare them side by side.
Alice and Bob each pick a secret number and publish g raised to it. Because
(gᵃ)ᵇ = (gᵇ)ᵃ, they both land on the same hour — the shared secret — while an
eavesdropper only ever sees the public values. Note: a prime of order 13 is
far too small for the real world; this is a toy. Real finite-field Diffie-Hellman
uses primes that are at least 2048 bits long.
Public: gᵃ mod p =
Public: gᵇ mod p =
Eve intercepts everything sent in the open. Here is all she has to work with:
To find the shared secret she must solve the discrete logarithm: recover
a from gᵃ. With a tiny modulus she can just try every value.