Problem with implementing Diffie-Hellman key exchange using GMP

Ismail Donmez ismail at kde.org.tr
Sat May 28 11:46:36 CEST 2005


Hi all,

I am using GMP since yesterday and I am trying to implement Diffie-Hellman key 
exchange with it. But the final values for the secret key is not same for 
each party. Code looks correct to my eyes. Maybe someone can tell me what I 
am doing wrong. Here is the test code :

===============
#include <gmpxx.h>
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
  mpz_class prime;
  mpz_class base=2;
  mpz_class secret_alice,secret_bob,calc_alice,calc_bob;

  prime = 
"127452162297611867695750099439441986191491647468315797199411404250764566218248343228532588048832328428773117232497828186086770\
50956745409379781245497526069657222703636504651898833151008222772087491045206203033063108075098874712912417029101508315117935752962862335\
062591404043092163187352352197487303798807791605274487594646923";

  gmp_randclass r_class (gmp_randinit_default);

  ifstream f("/dev/urandom");
  char data2[1081];
  f.read(data2,1080);
  r_class.seed((unsigned long)data2);
  f.close();

  secret_alice = r_class.get_z_bits(1080);
  secret_bob = r_class.get_z_bits(1080);

  
mpz_powm(calc_alice.get_mpz_t(),base.get_mpz_t(),secret_alice.get_mpz_t(),prime.get_mpz_t());
  
mpz_powm(calc_bob.get_mpz_t(),base.get_mpz_t(),secret_bob.get_mpz_t(),prime.get_mpz_t());

  cout << "Alice sends Bob " << calc_alice << endl;
  cout << "Bob sends Alice " << calc_bob << endl;

  
mpz_powm(calc_alice.get_mpz_t(),calc_bob.get_mpz_t(),secret_alice.get_mpz_t(),prime.get_mpz_t());
  
mpz_powm(calc_bob.get_mpz_t(),calc_alice.get_mpz_t(),secret_bob.get_mpz_t(),prime.get_mpz_t());

  cout << "Alice calculates: " << calc_alice << endl;
  cout << "Bob calculates:   " << calc_bob << endl;

  return 0;
}
================

Regards,
ismail

-- 
They say people don't believe in heroes anymore


More information about the gmp-discuss mailing list