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.io.*;
import java.math.BigInteger;

/*
 * 
 *  @ author Professional Cipher [www.professionalcipher.com]
 * 
 */

public class DEFFIE_HELLMAN {
 
 public static void main(String[]args)throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        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.println("R1="+R1);
        System.out.print("Enter value for y less than "+p+":");
        BigInteger y=new BigInteger(br.readLine());
        BigInteger R2=g.modPow(y,p);
        System.out.println("R2="+R2);
        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:
23
Enter primitive root of 23:5
Enter value for x less than 23:
7
R1=17
Enter value for y less than 23:8
R2=16
Key calculated at Alice's side:18
Key calculated at Bob's side:18
deffie hellman secret key Encryption has Taken


*/