Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Diffie-Helmann Key Exchange Java Program should take the size of the prime numbe

ID: 3681266 • Letter: D

Question

Diffie-Helmann Key Exchange

Java Program should take the size of the prime number p (in bits) as an argument during execution. Based on the size of p, your program should be able to compute and print the values of p, the generator g, and random numbers a, b Zp as per the Die-Hellman protocol. Note that all these values are selected randomly in your code. Based on the values chosen, your program should nally output the shared key generated.

(b). Suppose your program name is Assignment2.java. Here is a sample case of what your code is expected to output upon its execution

>> java Assignment 5

the value of p selected: 23

the value of g selected: 5

the value of a selected: 6

the value of b selected: 15

the value of shared key generated: 2

In this case, the program takes 5 as input meaning that the size of p is 5 bits. Note that here the values of p, g, a, b are assumed to be generated randomly as per the Die-Hellman protocol and therefore their values and the shared key value can be very dierent during each execution.

Explanation / Answer

import java.io.*;

import java.util.*;

public class DiffieExchange

{

public statuc void main(String [] args)

{

int p,g,a,b,kBob,kAlice,k,k1;

System.out.println("enter the prime number p");

Scanner s=new Scanner(System.in);

p=s.readLine();

System.out.println("enter the value for g which is less than p-1");

g=s.readLine();

System.out.println("enter the random value for a for Alice");

a=s.readLine();

System.out.println("enter the random value b for Bob");

b=s.readLine();

k=pow(g,a);

kAlice= k mod p;

k1= pow(g,b);

kBob= k1 mod p;

if(kAlice == kBob)

System.out.println("the shared kay for Alice and Bob is:"+kAlice);

} //end of main method

} //end of class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote