Java Program to implement Diffie Hellman Key Exchange Algorithm.

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 conceptual diagram illustrates the general idea of the key exchange by using colors instead of very large numbers.

This Program is based on Pune University BE IT Syllabus: Write program in C++ or Java to implement Diffie Hellman key exchangealgorithm.
Points to be remember before executing the program.
  • Create the Java Class with the name of : DEFFIE_HELLMAN
import java.math.BigInteger;

 *  @ author Professional Cipher []

public class DEFFIE_HELLMAN {
 public static void main(String[]args)throws IOException
        BufferedReader br=new BufferedReader(new InputStreamReader(;
        System.out.println("Enter prime number:");
        BigInteger p=new BigInteger(br.readLine());
        System.out.print("Enter primitive root of "+p+":");
        BigInteger g=new BigInteger(br.readLine());
        System.out.println("Enter value for x less than "+p+":");
        BigInteger x=new BigInteger(br.readLine());
        BigInteger R1=g.modPow(x,p);
        System.out.print("Enter value for y less than "+p+":");
        BigInteger y=new BigInteger(br.readLine());
        BigInteger R2=g.modPow(y,p);
        BigInteger k1=R2.modPow(x,p);
        System.out.println("Key calculated at Alice's side:"+k1);
        BigInteger k2=R1.modPow(y,p);
        System.out.println("Key calculated at Bob's side:"+k2);
        System.out.println("deffie hellman secret key Encryption has Taken");



Enter prime number:
Enter primitive root of 23:5
Enter value for x less than 23:
Enter value for y less than 23:8
Key calculated at Alice's side:18
Key calculated at Bob's side:18
deffie hellman secret key Encryption has Taken