Information and Network Security (INS)

Implementing Diffie-Hellman Algorithm

/*
	Program to implement Diffie-Hellman Algorithm.
	Author: PracsPedia		www.pracspedia.com
*/
import java.io.*;
class diffie
{
	public static void main(String args[])throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("Enter value any one prime number:");
		int prime_num=Integer.parseInt(br.readLine());
		
		System.out.println("Enter any one number which is less than " +prime_num);
		int primi_num=Integer.parseInt(br.readLine());
		
		System.out.println("Enter private key for Alice:");
		int pri_alice=Integer.parseInt(br.readLine());
		long public_alice=(long)Math.pow(primi_num,pri_alice)%prime_num;
		
		System.out.println("Enter private key for Bob:");
		int pri_bob=Integer.parseInt(br.readLine());
		long public_bob=(long)Math.pow(primi_num,pri_bob)%prime_num;
		
		System.out.println("Public key of Alice:" +public_alice);
		
		System.out.println("Public key of Bob:" +public_bob);

		System.out.println("Secret key of Alice:" +((long)Math.pow(public_bob,pri_alice)%prime_num));

		System.out.println("Secret key of Bob:" +((long)Math.pow(public_alice,pri_bob)%prime_num));


	}
}
Download Source Code Program List diffie-hellman output