A java program that demonstrates this key exchange protocol. Diffie–Hellman key exchange establishes a shared secret between two parties that can be used for secret communication for exchanging data over a public network. The following is the simplest implementation of the protocol.
Alice and Bob wish to communicate secretly over a public network. The following is how they do it.
- They publicly agree to use a modulus
p = 23
(a prime number) and a baseg = 5
(which is a primitive root modulo 23). - Alice chooses a secret integer
a = 4
, then sends Bobgᵃ mod p
.A = 5⁴ mod 23 = 4
- Bob chooses a secret integer
b = 3
, then sends Alicegᵇ mod p
.B = 5³ mod 23 = 10
- Alice computes
s = Bᵃ mod p
.s = 10⁴ mod 23 = 18
- Bob computes
s = Aᵇ mod p
.s = 4³ mod 23 = 18
- Alice and Bob now share a secret key (18).
Alice computed (gᵃ mod p)ᵇ mod p
and Bob computed (gᵇ mod p)ᵃ mod p
which are equivalent.
The encryption relies on the fact that the private numbers (a
or b
) can not be computed quickly, given only g
and p
, even by the fastest modern computers. Such a problem is called the discrete logarithm problem.
To make the given example more secure, larger values (600+ digits) of a
, b
, and p
need to be chosen.
Here is a list of prime numbers up to 1000 billion. for you to try.