Click here to Skip to main content
15,879,326 members
Articles / Desktop Programming / MFC
Article

Diffie-Hellman Key Exchange Example

Rate me:
Please Sign up or sign in to vote.
3.81/5 (30 votes)
14 Nov 2008CPOL2 min read 289.6K   9.6K   53   59
An example of how an encryption key can be shared by two users using the Diffie-Hellman key exchange approach.

Introduction

It's often required that a message be encrypted between two parties for secure communication. There are plenty of algorithms out there for encryption that are very secure, but their weakness lies in transporting the encryption key. The Diffie-Hellman key exchange protocol allows people to exchange keys in a manner that does not allow an eavesdropper to calculate the key in a fast manner.

This code demonstrates the use of this type of key exchange.

How to Use the Demo Project

To demonstrate the use of the key exchange, run two copies of the demo application. Set one to be the sender and the other to be a receiver.

The sender should generate the public keys, and the sender's interim key. Paste these values into the appropriate text boxes in the receiver application. The receiver should then click to generate his interim key, and copy this key into the "receiver's interim key" textbox on the sender application. Both applications should then be able to generate the same key by clicking "Generate Key".

Using the Source Code

The DiffieHellman class is simple to use and should be integrated in the following manner:

Make an instance of the class - (i.e. CDiffieHellman *DH = new CDiffieHellman;)

The sender application then does the following:

C++
__int64 n = 0;
__int64 g = 0;
__int64 SInterim = 0;
__int64 RInterim = 0;
__int64 key = 0; 

DH->CreateKeys(g,n);
DH->CreateSenderInterKey(SInterim);

//The sender now sends (n, g, and SInterim) to the receiving application
//This can be done unencrypted because they are public keys
//Now we wait until the reciever send us their interim key lets say RInterim

DH->CreateSenderEncryptionKey(key,RInterim);
//The shared encryption key is now the value of 'key'

The receiving application does the following:

C++
__int64 n = 0;
__int64 g = 0;
__int64 SInterim = 0;
__int64 RInterim = 0;
__int64 key = 0;

//Wait for the values of (n,g, and SInterim) to be sent here

DH->CreateRecipientInterKey(RInterim);

//Now send the RInterim key to the sender application

DH->CreateRecipientEncryptionKey(key,SInterim);
//The shared encryption key is now the value of 'key'

Extra Functions

There are some private member functions of the CDiffieHellman class that you may find useful, and please feel free to use them.

  • The GeneratePrime() function generates a large prime number.
  • The MillerRabin and IsItPrime functions can be used in conjunction to test primality.
  • The XtoYmodN is a function to raise x to the power of y in modulus n. Even though it sounds impossible for a computer to work out, say 150 million to the power of 150 million, this can be done in modulus n by using the power chaining method.

Further Help

Should you require any additional help, please do not hesitate to contact me. I would be interested in hearing your comments, suggestions and any questions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRFC 3526 Pin
Member 110532912-Sep-14 5:35
Member 110532912-Sep-14 5:35 
RFC 3526 has MOD groups with large primes for example MOD Group 5 :
CSS
The prime is: 2^1536 - 2^1472 - 1 + 2^64 * { [2^1406 pi] + 741804 }

   Its hexadecimal value is:

      FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1
      29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD
      EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245
      E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED
      EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D
      C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F
      83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
      670C354E 4ABC9804 F1746C08 CA237327 FFFFFFFF FFFFFFFF

   The generator is: 2.


Does this program support this? And how would I implement it?

Thanks in advance!
Tvoice
QuestionInterim Keys longer than 32 bits? Pin
christop_christop3-Jul-13 11:33
christop_christop3-Jul-13 11:33 
Questiondiffie_hillman Pin
Member 990145111-Mar-13 5:50
Member 990145111-Mar-13 5:50 
GeneralMy vote of 5 Pin
Member 43208448-Jun-12 11:22
Member 43208448-Jun-12 11:22 
Questionhow to run Pin
mohdamirr12-Feb-12 6:58
mohdamirr12-Feb-12 6:58 
GeneralInformation about Diffie hellman Pin
sudhakatr2-Feb-10 21:54
sudhakatr2-Feb-10 21:54 
GeneralRe: Information about Diffie hellman Pin
Griffter UK2-Feb-10 22:00
Griffter UK2-Feb-10 22:00 
Generalerror setting up the demo Pin
swati chaudhari25-Oct-09 17:23
swati chaudhari25-Oct-09 17:23 
GeneralRe: error setting up the demo Pin
Snehlata Shaw17-Nov-09 9:16
Snehlata Shaw17-Nov-09 9:16 
GeneralRe: error setting up the demo Pin
Griffter UK17-Nov-09 22:54
Griffter UK17-Nov-09 22:54 
GeneralLicense of this code Pin
Jonh Wendell13-Nov-08 3:35
Jonh Wendell13-Nov-08 3:35 
GeneralRe: License of this code Pin
Griffter UK13-Nov-08 5:32
Griffter UK13-Nov-08 5:32 
QuestionErrors Pin
zeeshan5114-Oct-08 22:14
zeeshan5114-Oct-08 22:14 
AnswerRe: Errors Pin
Griffter UK13-Nov-08 5:16
Griffter UK13-Nov-08 5:16 
Generalminor changes Pin
kalmiya8-Oct-08 10:37
kalmiya8-Oct-08 10:37 
GeneralCompile on linux Pin
Filipe Niero Felisbino2-Jun-08 8:33
Filipe Niero Felisbino2-Jun-08 8:33 
GeneralRe: Compile on linux Pin
Griffter UK2-Jun-08 23:21
Griffter UK2-Jun-08 23:21 
QuestionProblem compiling Pin
wygno29-Apr-07 17:58
wygno29-Apr-07 17:58 
AnswerRe: Problem compiling Pin
Griffter UK9-Apr-07 22:51
Griffter UK9-Apr-07 22:51 
QuestionEnquiry Pin
wygno28-Apr-07 0:46
wygno28-Apr-07 0:46 
AnswerRe: Enquiry Pin
Griffter UK9-Apr-07 22:49
Griffter UK9-Apr-07 22:49 
GeneralDiffie Hellman Key exchange Pin
Naeem Qazi21-Nov-06 9:44
Naeem Qazi21-Nov-06 9:44 
GeneralRe: Diffie Hellman Key exchange Pin
Garth J Lancaster21-Nov-06 10:23
professionalGarth J Lancaster21-Nov-06 10:23 
GeneralRe: Diffie Hellman Key exchange Pin
Naeem Qazi21-Nov-06 23:29
Naeem Qazi21-Nov-06 23:29 
GeneralRe: Diffie Hellman Key exchange Pin
Hashbullet30-Mar-07 1:09
Hashbullet30-Mar-07 1:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.