Sunday, 24 March 2019

DIFFIE HELLMAN KEY EXCHANGE

import java.math.*;
import java.io.*;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
    System.out.println("Enter a public key p");
    BigInteger p=new BigInteger(s.next());
    System.out.println("Enter public key g");
    BigInteger g=new BigInteger(s.next());
    System.out.println("Enter the private key at alice side");
    BigInteger a=new BigInteger(s.next());
    System.out.println("Enter private  key at bobs side");
    BigInteger b=new BigInteger(s.next());
    BigInteger x=new BigInteger("0");
    BigInteger y=new BigInteger("0");
    
    x=g.modPow(a,p);
    System.out.println("Key generated at Alice side is: "+x);
        y=g.modPow(b,p);
    System.out.println("Key generated at Bobs side is: "+y);
    System.out.println("After key exchange :");
    System.out.println("Secret KEy shared at Alice side is: "+y.modPow(a,p));
   System.out.println("Secret KEy shared at Bobs  side is: "+x.modPow(b,p));

}
}

No comments:

Post a Comment

FERMATS LITTLE THEOREM

import java.math.*; import java.io.*; import java.util.Scanner; public class Main { public static void main(String[] args) {    Sca...