Suppose that Alice and Bob want to share a private-key based on the Die-Hellman
ID: 3741537 • Letter: S
Question
Suppose that Alice and Bob want to share a private-key based on the
Die-Hellman key exchange protocol. Let us assume that they have agreed on the domain
parameters p = 6779 and g = 3, where p and g denote the prime group and generator,
respectively. Further, suppose that Alice chooses the secret value a = 384 and Bob chooses
the secret value b = 152. Write a program that takes the above parameters and performs
simulated key exchange as per the Die-Hellman key exchange protocol. Your program
should output intermediate results and the nal private-key computed by Alice and Bob.
Any programming language is fine.
Explanation / Answer
public class DiffieHellman{
public static void main(String []args){
int p = 6779, g = 3;
//Secret keys a-Alice, b-Bob
int a = 384, b = 152;
int i, s_Bob = 1, s_Alice = 1;
int AliceToBob = 1;
//Alice computes g^a mod p and sends to Bob
for(i=0; i<a; i++) {
AliceToBob *= g;
AliceToBob %= p;
}
System.out.println(" Alice sends Bob "+AliceToBob);
int BobToAlice = 1;
//Bob computes g^b mod p and sends to Alice
for(i=0; i<b; i++) {
BobToAlice *= g;
BobToAlice %= p;
}
System.out.println(" Bob sends Alice "+BobToAlice);
System.out.println(" Bob Receives "+AliceToBob);
//Bob computes (received)^b mod p
for(i=0; i<b; i++) {
s_Bob *= AliceToBob;
s_Bob %= p;
}
System.out.println(" Bob computes "+s_Bob);
System.out.println(" Alice Receives "+BobToAlice);
//Alice computes (received)^a mod p
for(i=0; i<a; i++) {
s_Alice *= BobToAlice;
s_Alice %= p;
}
System.out.println(" Alice computes "+s_Alice);
//Both the computed results should be the same and the key is shared
if(s_Alice == s_Bob) {
System.out.println(" Alice and Bob now share the secret key: "+s_Alice);
}
}
}
Sample Output: