C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Diffie-Hellman-Merkle Key Exchange Program

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Protoman
Guest





PostPosted: Sun Jul 30, 2006 9:11 am    Post subject: Diffie-Hellman-Merkle Key Exchange Program Reply with quote



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(;Wink
{
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!!!!
Back to top
Kai-Uwe Bux
Guest





PostPosted: Sun Jul 30, 2006 9:11 am    Post subject: [OT] Re: Diffie-Hellman-Merkle Key Exchange Program Reply with quote



Protoman wrote:

Quote:
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;

on topic remarks:
=================

If this loop overflows, you have undefined behavior.

You should use unsigned long long instead. Then it will wrapand give the
correct result mod 2^N where N is the bitlength.

However, even that will not do, for reasons explained below.


Quote:
return i;
}

int main()
{
long long A,B;
long long base,mod;
for(;Wink
{
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;

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.

Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.