Re: Diffie-Hellman source code

205 views
Skip to first unread message

Renzo Tomaselli

unread,
Apr 23, 2002, 3:42:45 AM4/23/02
to crypto...@eskimo.com
Hi,
    I'm interested into this issue as well. If anyone can offer such an example, please post it to the list.
Thanks,
                                             Renzo Tomaselli     
---------------------------------------------------------------------------
TecnoTP s.n.c. Special Information System Design
Maso Pelauchi I38050 Ronchi Valsugana,  Trento TN  ITALY
Tel. +39 0461 773164      Fax. +39 0461 771514
e-mail: renzo.t...@tecnotp.it  
---------------------------------------------------------------------------
----- Original Message -----
Sent: martedì 23 aprile 2002 00:36
Subject: Diffie-Hellman source code

    Could somebody send me some simple source code on how to correctly implement a Diffie-Hellman key agreement class (DH) using Crypto++ with all the neccesary operations around ?
    Which random number generator should I use in my program instead of LC_RNG ?
 
I'll be grateful for every response.
 
 

---
Outgoing message doesn't include viruses.
Zkontrolováno antivirovým systémem AVG (http://www.grisoft.cz).
Verze: 6.0.343 / Virová báze: 190 - datum vydání: 22. 3. 2002

Gavin Tan

unread,
Apr 23, 2002, 11:22:54 PM4/23/02
to crypto...@eskimo.com
Here's my source code. It uses MFC classes.
It may NOT be correct.

Hi Wei Dai,
both Alice's and Bob's private keys contain many zeroes. Is that expected?
What is the typical key bit-length for Diffie-Hellman to be considered
secure?

If anyone knows the answers to the above questions, could you please reply
too?

Thank you
Gavin

////////////////////////////////////////////////////////////////////////
int iCounter = 0;
int iCounter2 = 0;
CString sText = "";
CString sText2 = "";

// Diffie Hellman (For posting)
// Alice
// Create auto seeded random number generator
CryptoPP::AutoSeededRandomPool autorng = CryptoPP::AutoSeededRandomPool();

// Create diffie hellman class
CryptoPP::DH dhDHA = CryptoPP::DH(autorng, 128);

// Get prime and generator
CryptoPP::Integer integerPrimeA = dhDHA.GetPrime();
CryptoPP::Integer integerGeneratorA = dhDHA.GetGenerator();

// Store prime and generator values
byte acPrimeA[16] = {0};
for (iCounter = 0; iCounter < 16; iCounter++)
{
acPrimeA[iCounter] = integerPrimeA.GetByte(iCounter);
}
byte cGeneratorA = integerGeneratorA.GetByte(0);

// Generate key pair
byte acPrivateKeyA[16] = {0};
byte acPublicKeyA[16] = {0};
dhDHA.GenerateKeyPair(autorng, acPrivateKeyA, acPublicKeyA);

// Display key pair
sText2 = "Alice:\nPrivate key is:\n";
for (iCounter = 0; iCounter < 16; iCounter++)
{
sText.Format("%02X", acPrivateKeyA[iCounter]);
sText2 = sText2 + sText;
}
sText2 = sText2 + "\nPublic key is:\n";
for (iCounter = 0; iCounter < 16; iCounter++)
{
sText.Format("%02X", acPublicKeyA[iCounter]);
sText2 = sText2 + sText;
}
MessageBox(sText2);

// Bob
// Get prime and generator from Alice
CryptoPP::Integer integerPrimeB;
for (iCounter = 0; iCounter < 16; iCounter++)
{
integerPrimeB.SetByte(iCounter, acPrimeA[iCounter]);
}
CryptoPP::Integer integerGeneratorB;
integerGeneratorB.SetByte(0, cGeneratorA);

// Create diffie hellman class based on Alice's p and g
CryptoPP::DH dhDHB = CryptoPP::DH(integerPrimeB, integerGeneratorB);

// Generate key pair
byte acPrivateKeyB[16] = {0};
byte acPublicKeyB[16] = {0};
dhDHB.GenerateKeyPair(autorng, acPrivateKeyB, acPublicKeyB);

// Display key pair
sText2 = "Bob:\nPrivate key 2 is:\n";
for (iCounter = 0; iCounter < 16; iCounter++)
{
sText.Format("%02X", acPrivateKeyB[iCounter]);
sText2 = sText2 + sText;
}
sText2 = sText2 + "\nPublic key 2 is:\n";
for (iCounter = 0; iCounter < 16; iCounter++)
{
sText.Format("%02X", acPublicKeyB[iCounter]);
sText2 = sText2 + sText;
}
MessageBox(sText2);

// To calculate agreed value between Alice and Bob
byte acAgreedValueA[16] = {0};
dhDHA.Agree(acAgreedValueA, acPrivateKeyA, acPublicKeyB, TRUE);
byte acAgreedValueB[16] = {0};
dhDHB.Agree(acAgreedValueB, acPrivateKeyB, acPublicKeyA, TRUE);

// Display both agreed values for comparison
sText2 = "Alice's agreed value is:\n";
for (iCounter = 0; iCounter < 16; iCounter++)
{
sText.Format("%02X", acAgreedValueA[iCounter]);
sText2 = sText2 + sText;
}
sText2 = sText2 + "\nBob's agreed value is:\n";
for (iCounter = 0; iCounter < 16; iCounter++)
{
sText.Format("%02X", acAgreedValueB[iCounter]);
sText2 = sText2 + sText;
}
MessageBox(sText2);
////////////////////////////////////////////////////////////////////////


----Original Message Follows----
From: "Renzo Tomaselli" <renzo.t...@tecnotp.it>
Reply-To: crypto...@eskimo.com
To: <crypto...@eskimo.com>
Subject: Re: Diffie-Hellman source code
Date: Tue, 23 Apr 2002 09:45:17 +0200

Tomas, dyn...@globtelnet.sk


_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com

Joern Seger

unread,
Apr 24, 2002, 2:27:29 AM4/24/02
to crypto...@eskimo.com
Wouldn't it a good idea to include some examples like this on the homepage?
My personal interest is more in how to use this crypto-library than in
structure of methods, templates and classes (like shown in the doxy-file).
I know that security is a difficult thing and must be implemented carefully
but examples make it much easy to understand functionality and to get deeper
into the material.

Jörn

Tomas Holly

unread,
Apr 22, 2002, 6:44:49 PM4/22/02
to crypto...@eskimo.com
Reply all
Reply to author
Forward
0 new messages