In: Computer Science
Implement a Java program for Diffie-Hellman manually insert p, g, a, and b to get shared key.
Write a Java
I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.
--------------------solution.java-------------------
import java.util.*;
import java.math.BigInteger; //to stor big integers
public class solution{
public static void main(String[] args){
Scanner scan = new Scanner(
System.in );
System.out.print("\nEnter p:
");
BigInteger p =
scan.nextBigInteger(); //read p
System.out.print("\nEnter g:
");
BigInteger g =
scan.nextBigInteger(); //read g
System.out.print("\nEnter a: ");
//read a
int a = scan.nextInt();
System.out.print("\nEnter b: ");
//read b
int b = scan.nextInt();
BigInteger Shared_key = ((g.pow(a).mod(p)).pow(b)).mod(p); //shared key = g^(ab)mod(p)
System.out.print("\nShared_key =
" + Shared_key + "\n");
}
}
--------------------Screenshots solution.java-------------------
--------------------Output------------------
----------------------------------------------------
I hope this helps you,
Please rate this answer if it helped you,
Thanks for the opportunity