432,483 Members | 1,052 Online
Need help? Post your question and get tips & solutions from a community of 432,483 IT Pros & Developers. It's quick & easy.

# Diffie-Hellman-Merkle Key Exchange Program

 P: n/a 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 #include 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(;ibase; 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!!!! Jul 30 '06 #1
4 Replies

 P: n/a Protoman wrote: 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 #include 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(;ibase; 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; more off topic remarks: ======================= when you take the overflown result from Exp(base,A) and pass it to %mod, you do not really compute the correct remainder because for x 2^N, in general x % mod != ( x % 2^N ) % mod You need to either use a library for arbitrary precision integers or write a function exp_mod( base, exponent, modulus ) that computes base**exponent mod modulus correctly. 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; } Also, your question is on the borderline to purely algorithmic problems. You might want to ask in comp.programming instead. Best Kai-Uwe Bux Jul 30 '06 #2

 P: n/a Kai-Uwe Bux wrote: Protoman wrote: 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 #include 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(;ibase; 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; more off topic remarks: ======================= when you take the overflown result from Exp(base,A) and pass it to %mod, you do not really compute the correct remainder because for x 2^N, in general x % mod != ( x % 2^N ) % mod You need to either use a library for arbitrary precision integers or write a function exp_mod( base, exponent, modulus ) that computes base**exponent mod modulus correctly. 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; } Also, your question is on the borderline to purely algorithmic problems. You might want to ask in comp.programming instead. Best Kai-Uwe Bux Did what you said, still comes up w/different values. Code: #include #include using std::cout; using std::cin; using std::endl; using std::system; unsigned long long ExpMod(const unsigned long long& base,const unsigned long long& exp, const unsigned long long& mod) { unsigned long long i=1; for(;ibase; cout << "Modulus: " << endl; cin >mod; cout << "Alice, choose your secret number: " << endl; cin >A; cout << "Bob, choose your secret number: " << endl; cin >B; unsigned long long a=ExpMod(base,A,mod); unsigned long long b=ExpMod(base,B,mod); unsigned long long akey=ExpMod(b,A,mod); unsigned long long bkey=ExpMod(a,B,mod); cout << "Alice's key: " << akey << endl; cout << "Bob's key: " << bkey << endl; } system("PAUSE"); return EXIT_SUCCESS; } Jul 30 '06 #3

 P: n/a Protoman wrote: > Kai-Uwe Bux wrote: >Protoman wrote: 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 #include 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(;ibase; 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; more off topic remarks:=======================when you take the overflown result from Exp(base,A) and pass it to %mod,you do not really compute the correct remainder because for x 2^N, ingeneral x % mod != ( x % 2^N ) % modYou need to either use a library for arbitrary precision integers orwrite a function exp_mod( base, exponent, modulus ) that computes base**exponent mod moduluscorrectly. 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; } Also, your question is on the borderline to purely algorithmic problems.You might want to ask in comp.programming instead. BestKai-Uwe Bux Did what you said, still comes up w/different values. Code: #include #include using std::cout; using std::cin; using std::endl; using std::system; unsigned long long ExpMod(const unsigned long long& base,const unsigned long long& exp, const unsigned long long& mod) { unsigned long long i=1; for(;i int main() { unsigned long long A,B; unsigned 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; unsigned long long a=ExpMod(base,A,mod); unsigned long long b=ExpMod(base,B,mod); unsigned long long akey=ExpMod(b,A,mod); unsigned long long bkey=ExpMod(a,B,mod); cout << "Alice's key: " << akey << endl; cout << "Bob's key: " << bkey << endl; } system("PAUSE"); return EXIT_SUCCESS; } Best Kai-Uwe Jul 30 '06 #4

 P: n/a Kai-Uwe Bux wrote: Protoman wrote: Kai-Uwe Bux wrote: Protoman wrote: 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 #include 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(;ibase; 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; more off topic remarks: ======================= when you take the overflown result from Exp(base,A) and pass it to %mod, you do not really compute the correct remainder because for x 2^N, in general x % mod != ( x % 2^N ) % mod You need to either use a library for arbitrary precision integers or write a function exp_mod( base, exponent, modulus ) that computes base**exponent mod modulus correctly. 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; } Also, your question is on the borderline to purely algorithmic problems. You might want to ask in comp.programming instead. Best Kai-Uwe Bux Did what you said, still comes up w/different values. Code: #include #include using std::cout; using std::cin; using std::endl; using std::system; unsigned long long ExpMod(const unsigned long long& base,const unsigned long long& exp, const unsigned long long& mod) { unsigned long long i=1; for(;ibase; cout << "Modulus: " << endl; cin >mod; cout << "Alice, choose your secret number: " << endl; cin >A; cout << "Bob, choose your secret number: " << endl; cin >B; unsigned long long a=ExpMod(base,A,mod); unsigned long long b=ExpMod(base,B,mod); unsigned long long akey=ExpMod(b,A,mod); unsigned long long bkey=ExpMod(a,B,mod); cout << "Alice's key: " << akey << endl; cout << "Bob's key: " << bkey << endl; } system("PAUSE"); return EXIT_SUCCESS; } Best Kai-Uwe It works!!!!! Praise God, you did it!!!!!! Thank you so much!!!!!!!! Jul 30 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

Browse more C / C++ Questions on Bytes