P
Protoman
I'm writing a Diffie-Hellman-Merkle Key Exchange Program, and,
sometimes (I haven't figured out how to predict it yet), Alice's and
Bob's shared symmetric key are different! This shouldn't happen!
Code:
#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::system;
long long Exp(const long long& base,long long exp)
{
long long i=1;
for(;i<exp;i++)
i*=base;
return i;
}
int main()
{
long long A,B;
long long base,mod;
for(;
{
cout << "Base: " << endl;
cin >> base;
cout << "Modulus: " << endl;
cin >> mod;
cout << "Alice, choose your secret number: " << endl;
cin >> A;
cout << "Bob, choose your secret number: " << endl;
cin >> B;
long long a=Exp(base,A)%mod;
long long b=Exp(base,B)%mod;
cout << "Alice's value: " << a << endl;
cout << "Bob's value: " << b << endl;
long long akey=Exp(b,A)%mod;
long long bkey=Exp(a,B)%mod;
cout << "Alice's key: " << akey << endl;
cout << "Bob's key: " << bkey << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Here's an link to explain DHM Key Exchange:
http://en.wikipedia.org/wiki/Diffie-Hellman
Thanks!!!!
sometimes (I haven't figured out how to predict it yet), Alice's and
Bob's shared symmetric key are different! This shouldn't happen!
Code:
#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::system;
long long Exp(const long long& base,long long exp)
{
long long i=1;
for(;i<exp;i++)
i*=base;
return i;
}
int main()
{
long long A,B;
long long base,mod;
for(;
{
cout << "Base: " << endl;
cin >> base;
cout << "Modulus: " << endl;
cin >> mod;
cout << "Alice, choose your secret number: " << endl;
cin >> A;
cout << "Bob, choose your secret number: " << endl;
cin >> B;
long long a=Exp(base,A)%mod;
long long b=Exp(base,B)%mod;
cout << "Alice's value: " << a << endl;
cout << "Bob's value: " << b << endl;
long long akey=Exp(b,A)%mod;
long long bkey=Exp(a,B)%mod;
cout << "Alice's key: " << akey << endl;
cout << "Bob's key: " << bkey << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Here's an link to explain DHM Key Exchange:
http://en.wikipedia.org/wiki/Diffie-Hellman
Thanks!!!!