0
1.8kviews
Diffie Hellman Key Exchange program in java language.

Show steps of the Diffie Hellman Key Exchange algorithm with one solved example.

Write the java program for the Diffie Hellman Key Exchange that accepts the value for P and G from the user.

But, randomly generate the private key values for Alice and Bob and calculate Secret Key SA and SB from both sides.

Also, show randomly generated values for private keys (PA, PB), calculated values for public keys (pa, pb), and finally secret keys (SA, SB)

Test the program for following Three examples:

a] P = 7, G = 17

b] P = 17, G = 5

c] P = 5, G = 3

1 Answer
0
65views

Diffie Hellman Key Exchange Algorithm

Step 1 -

  • Both parties say Alice and Bob initially agreed on the two values 'P' and 'G'.
  • Where P is a prime number and G is the generator or primitive root of a prime number.
  • In that value of P must be greater than G.(P > G)

Step 2 -

  • Both parties decided their private key values individually saying here 'PA' and 'PB' for Alice and Bob respectively.

Step 3 -

  • Based on their private key values both Alice and Bob calculate their respective public key values 'pa' and 'pb' at their side and exchange them with each other.

$$ pa = G^{PA} Mod\ P $$

$$ pb = G^{PB} Mod\ P $$

Step 4 -

  • After receiving Bob's public key 'pb' Alice calculated Secret Key called 'SA' using their private key 'PA'.

$$ SA = pb^{PA} Mod\ P $$

  • After receiving Alice's public key 'pa' Bob calculated Secret Key called 'SB' using their private key 'PB'.

$$ SB = pa^{PB} Mod\ P $$

  • Finally, both the parties Alice and Bob obtain the same value of the secret key.

Solved Example -

Step 1 -

  • Alice & Bob both agreed on the values of P = 7 and G = 3

Step 2 -

  • Alice & Bob decides their respective private key values PA = 2 and PB = 5 separately.

Step 3 -

  • Alice & Bob calculates their public key values at their sides as follows:

$$ pa = G^{PA} Mod\ P = 3^2 Mod\ 7 = 2 $$

$$ pb = G^{PB} Mod\ P = 3^5 Mod\ 7 = 5 $$

  • After calculating Alice & Bob exchange their public key values with each other.

Step 4 -

  • After receiving Bob's public key 'pb' Alice calculated Secret Key called 'SA' using their private key 'PA'.

$$ SA = pb^{PA} Mod\ P = 5^2 Mod\ 7 = 4 $$

  • After receiving Alice's public key 'pa' Bob calculated Secret Key called 'SB' using their private key 'PB'.

$$ SB = pa^{PB} Mod\ P = 2^5 Mod\ 7 = 4 $$

  • Both the parties get the same value for Secret Key, that is 4.

Java Program for Diffie Hellman

  • The below java program takes only two inputs from the user for Modulus Prime 'P' and Primitive Root or Generator value 'G'.
  • Private key values for Alice and Bob were computed randomly using the Random Class method.
  • Then computed Public key and Secret key values using the Math.pow() function with modulo (%) operator.
  • Finally, the program shows the output that contains the following things:
    • Randomly computed Private Key Values (PA, PB) for both Alice and Bob.
    • Computed Public Key Values (pa, pb) for both Alice and Bob.
    • Computed Secret Key Values (SA, SB) from both Alice and Bob.
import java.util.*;
import java.lang.*;     
class DiffieHellman
{  
public static void main(String[] args)  
{          
int P, G, PA, PB, pa, pb, SA, SB;  
Scanner sc = new Scanner(System.in); 
Random random = new Random();  
System.out.println("Initial Agreed values for Modulus Prime P and Generator Value G from Alice and Bob");  
System.out.println("\nEnter the value for Modulus Prime P = ");  
P = sc.nextInt();  
System.out.println("Enter the value for Generator Value G = ");  
G = sc.nextInt();  
if (P > G)
{
PA = 1 + random.nextInt(9);
System.out.println("\nAlice randomly decides its Private Key value PA = " + PA);   
PB = 1 + random.nextInt(9);
System.out.println("Bob randomly decides its Private Key value PB = " + PB);  
pa = (int)Math.pow(G,PA)%P;  
System.out.println("\nAlice computed their public key value pa = " + pa); 
pb = (int)Math.pow(G,PB)%P;   
System.out.println("Bob computed their public key value pb = " + pb);   
System.out.println("\nAlice and Bob EXCHANGED their computed public key values with each other");  
SA = (int)Math.pow(pb,PA)%P; 
SB = (int)Math.pow(pa,PB)%P; 
System.out.println("\nSecret key value SA computed by Alice = " + SA);  
System.out.println("Secret key value SB computed by Bob = " + SB);  
}
else
{
System.out.println("\nERROR !!! G > P");
}
}  
}  

Output for the above Java Program for the given Test Cases:

a] P = 7, G = 17

P = 7, G = 17

b] P = 17, G = 5

P = 17, G = 5

c] P = 5, G = 3

P = 5, G = 3

Please log in to add an answer.