One of the main challenges to use a security key algorithm such as RC4 is the ke
ID: 3917470 • Letter: O
Question
One of the main challenges to use a security key algorithm such as RC4 is the key exchange. Very often the key exchange must be done offline; otherwise you run the risk of the key being eavesdropped by a hacker.
Now implement Diffie-Hellman Key Exchange Algorithm in C++. A user will be asked to enter two prime numbers g and p, in the range 4000 to 10000 (https://primes.utm.edu/lists/small/10000.txt). Then user A enters a number a, and user B enters a number b, both in the range of 40 to 100. Now compute the security keys generated by user A and user B respectively, and verify they are the same. Be aware that if you use the pow() function or something similar, your calculation will most likely go out of bounds. As any good program would do, your program should verify the inputs.
Submit the following:
1. Source code
2. A screen shot showing the keys generated by A and B.
Explanation / Answer
#include<iostream>
using namespace std;
long long int power(long long int a, long long int b,long long int P)
{ long long int num=a;
if (b == 1)
return a;
else
{
while(b>1){
a*=num;
a%=P;
b--;
}
return a;
}
}
int main()
{
long long int P, G, x, a, y, b, ka, kb;
cout<<"Enter P : ";
cin>>P;
cout<<"The value of P :"<< P<<" ";
cout<<"Enter G : ";
cin>>G;
cout<<"The value of G : "<< G<<" ";
cout<<"Enter private key of Alice : ";
cin>>a;
cout<<"The private key a for Alice : "<<a<<" ";
x = power(G, a, P);
cout<<"Enter private key of Bob : ";
cin>>b;
cout<<"The private key a for Bob : "<<b<<" ";
y = power(G, b, P);
ka = power(y, a, P);
kb = power(x, b, P);
cout<<"Secret key for the Alice is : "<< ka<<" ";
cout<<"Secret Key for the Bob is : "<<kb<<" ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.