Algorithm/Steps:
1. A and B get public numbers P = 23, G = 9
2. A selected a private key a = 4 and B
selected a private key b = 3
3. A and B compute public values
A: x =(9^4 mod 23) = (6561 mod 23) = 6
B: y = (9^3 mod 23) = (729 mod 23) = 16
4. A and B exchange public numbers
5. A receives public key y =16 and B
receives public key x = 6
6. A and B compute symmetric keys
A: ka = y^a mod p = 65536 mod 23 = 9
B: kb = x^b mod p = 216 mod 23 = 9
7.
9 is the shared secret.
Code:
import
java.util.*;
import
java.lang.*;
import
java.math.BigInteger;
class Main
{
public static void main(String
args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the
value of Xa&Xb");
BigInteger Xa= BigInteger.valueOf(sc.nextInt());
BigInteger Xb= BigInteger.valueOf(sc.nextInt());
System.out.println("Enter a Prime
no. p");
BigInteger p = BigInteger.valueOf(sc.nextInt());
System.out.println("Enter
Primitive Root a, such that a<p");
BigInteger a = BigInteger.valueOf(sc.nextInt());
BigInteger Ya= a.pow(Xa.intValue()).mod(p);
BigInteger Yb= a.pow(Xb.intValue()).mod(p);
BigInteger Ka = Yb.pow(Xa.intValue()).mod(p);
BigInteger Kb= Ya.pow(Xb.intValue()).mod(p);
if(Ka.compareTo(Kb)==0)
{
System.out.println("Transmission
successful"+Ya+" "+Yb+" "+Ka+" "+Kb);}
else{
System.out.println("Transmission
failed"+Ya+" "+Yb+" "+Ka+" "+Kb);}
}
}
No comments:
Post a Comment