Diffie Hellman Key Exchange Java Example
Diffie Hellman Key Exchange program in java language. Show steps of the Diffie Hellman Key Exchange algorithm with one solved example. Write the java program for the Diffie Hellman Key Exchange that accepts the value for P and G from the user. But, randomly generate the private key values for Alice and Bob and calculate Secret Key SA and SB from both sides. Also, show randomly generated values for private keys (PA, PB), calculated values for public keys (pa, pb), and finally secret keys (SA, SB) Test the program for following Three examples: a] P = 7, G = 17 b] P = 17, G = 5 c] P = 5, G = 3 1 Answer
written 10 weeks ago by • modified 10 weeks ago
Step 1 - Step 2 - Step 3 - $$ pa = G^{PA} Mod\ P $$ $$ pb = G^{PB} Mod\ P $$ Step 4 - $$ SA = pb^{PA} Mod\ P $$ $$ SB = pa^{PB} Mod\ P $$ Solved Example - Step 1 - Step 2 - Step 3 - $$ pa = G^{PA} Mod\ P = 3^2 Mod\ 7 = 2 $$ $$ pb = G^{PB} Mod\ P = 3^5 Mod\ 7 = 5 $$ Step 4 - $$ SA = pb^{PA} Mod\ P = 5^2 Mod\ 7 = 4 $$ $$ SB = pa^{PB} Mod\ P = 2^5 Mod\ 7 = 4 $$ Output for the above Java Program for the given Test Cases: a] P = 7, G = 17 b] P = 17, G = 5 c] P = 5, G = 3
written 10 weeks ago by • modified 10 weeks ago Diffie Hellman Key Exchange Algorithm
Java Program for Diffie Hellman
import java.util.*; import java.lang.*; class DiffieHellman { public static void main(String[] args) { int P, G, PA, PB, pa, pb, SA, SB; Scanner sc = new Scanner(System.in); Random random = new Random(); System.out.println("Initial Agreed values for Modulus Prime P and Generator Value G from Alice and Bob"); System.out.println("\nEnter the value for Modulus Prime P = "); P = sc.nextInt(); System.out.println("Enter the value for Generator Value G = "); G = sc.nextInt(); if (P > G) { PA = 1 + random.nextInt(9); System.out.println("\nAlice randomly decides its Private Key value PA = " + PA); PB = 1 + random.nextInt(9); System.out.println("Bob randomly decides its Private Key value PB = " + PB); pa = (int)Math.pow(G,PA)%P; System.out.println("\nAlice computed their public key value pa = " + pa); pb = (int)Math.pow(G,PB)%P; System.out.println("Bob computed their public key value pb = " + pb); System.out.println("\nAlice and Bob EXCHANGED their computed public key values with each other"); SA = (int)Math.pow(pb,PA)%P; SB = (int)Math.pow(pa,PB)%P; System.out.println("\nSecret key value SA computed by Alice = " + SA); System.out.println("Secret key value SB computed by Bob = " + SB); } else { System.out.println("\nERROR !!! G > P"); } } }
Please log in to add an answer.
Source: https://www.ques10.com/p/66396/diffie-hellman-key-exchange-program-in-java-lang-1/
0 Response to "Diffie Hellman Key Exchange Java Example"
Post a Comment