Diffie-Hellman algorithm solved examples

The Diffie-Hellman key exchange algorithm establishes a shared secret key between two parties over an unsecured communication channel.

In this article, we will solve a couple of Diffie-Hellman key exchange algorithm examples. It will give you a better idea about this algorithm.

Example 1

John and Ricky use the Diffie-Hellman protocol with a common prime number p = 11 and a primitive root g = 6. If John’s private key ‘a’ = 3 and Ricky’s private key ‘b’ = 2 then find out:

  1. John’s public key ‘A’
  2. Ricky’s public key ‘B’
  3. Shared secret key

Step 1. Calculate John’s public key ‘A’ using:

You can use this online Modulo calculator to find out the mod.

Step 2. Calculate Ricky’s public key ‘B’ using:

Step 3. Calculate John’s shared secret key using Ricky’s public key B:

Shared secret key of John = 5

Step 4. Calculate Ricky’s shared secret key using John’s public key A:

Shared secret key of Ricky = 5

John and Ricky have the same shared secret key i.e. 5 without transferring their private key.

Example 2

User 1 and User 2 decided to use the Diffie-Hellman algorithm to establish a shared secret key. They selected their prime number p = 13 and its primitive root g = 2. User 1’s private key is 5 and User 2’s private key is 6. Find out:

  1. The public key of User 1 & User 2
  2. Shared secret key between User 1 & User 2

Step 1: Calculate the public key of User 1:

Let the public key of User 1 be ‘A’.

Step 2: Calculate the public key of User 2:

Let the public key of User 2 be ‘B’:

Step 3: Calculate User 1’s shared secret using User 2’s public key B:

Step 4: Calculate User 2’s shared secret key using User 1’s public key A:

Hence, both User 1 and User 2 have the same shared secret key i.e. 12.

Example 3

Alice and Bob want to establish a shared secret key using Diffie-Hellman key exchange protocol. Both decided on a prime number ‘p’ as 23 and its primitive root ‘g’ as 5. Alice’s private key is 9 and Bob’s private key is 11. Find out the public key of Alice & Bob and also the shared secret key between them.

Step 1: Calculate Alice’s public key using:

Let Alice’s public key be ‘A’.

Alice’s public key = 11

Step 2: Calculate Bob’s public key using:

Let Bob’s public key be ‘B’.

= 48828125 mod 23

Bob’s public key = 22

Step 3: Calculate Alice’s shared secret key using Bob’s public key B:

= 1,207,269,217,792 mod 23

Alice’s shared secret key = 22

Step 4: Calculate Bob’s shared secret key using Alice’s public key A:

= 285,311,670,611 mod 23

Bob’s shared secret key = 22

Hence, Alice and Bob have the same shared secret key i.e. 22 using Diffie-Hellman key exchange algorithm.

If you found any error in the above examples, please let us know in the comment section below.