who can give me a lmplement of Diffie-Hellman key exchange.

S

say88

C or C++ version,os independent,can be used
under linux and windows.
Thanks for advanced.
 
M

marcas

say88 said:
C or C++ version,os independent,can be used
under linux and windows.
Thanks for advanced.

Looks like homework, but here is the basic idea:


#include <cmath>
#include <iostream>

using namespace std;

int pow (int b, int p) {
return (int) pow ((double)b,(double)p); // brutal
}

int main () {

int p=13; //public prime
int g=2; //public 2 =< g =< p-2

int alice_a=44; //secret
int bob_b=22; //secret

int alice_A=pow(g,alice_a)%p; //sends to bob
int bob_B=pow(g,bob_b)%p; // sends to alice


cout << "Bob calculates key:" << pow(alice_A,bob_b)%p << endl;
cout << "Alice calculates key:" << pow(bob_B,alice_a)%p << endl;

return 0;
}

regards marcas
 
B

BobR

marcas wrote in message said:
#include <cmath> // C++
#include <iostream>
using namespace std;

int pow (int b, int p) {
return (int) pow ((double)b,(double)p); // brutal
}

????? paren's in wrong place? C style cast in C++ pgm?

int pow(int b, int p){
return int( pow(double( b ), double( p ) ) ); // brutal + brutal
}

<G>
 
M

marcas

BobR schrieb:

????? paren's in wrong place? C style cast in C++ pgm?

int pow(int b, int p){
return int( pow(double( b ), double( p ) ) ); // brutal + brutal
}

<G>

yes, you are right, it should be

int pow (int b, int p) {
return static_cast<int>
(pow(static_cast<double>(b),static_cast<double>(p)));
}

to view that here happens something very bad
regards marcas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top