Wednesday, 9 January 2019

Java program to implement Diffie Hellman Algorithm (CSS)


import java.io.*;
import java.math.BigInteger;

public class Deffie{
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 Alice private kay 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 Bob private key 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");
}

}

Output:

codept@codept-04 ~/Desktop/chan $ java Deffie
Enter prime number:
23
Enter primitive root of 23:5
Enter value for Alice private kay less than 23:
4
R1=4
Enter value for Bob private key less than 23:7
R2=17
Key calculated at Alice's side:8
Key calculated at Bob's side:8
deffie hellman secret key Encryption has Taken

No comments:

Post a Comment