Click here to Skip to main content
Click here to Skip to main content

Diffie-Hellman Key Exchange Example

, 14 Nov 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

__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:

__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)

Share

About the Author

Griffter UK
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionRFC 3526 PinmemberMember 110532912-Sep-14 6:35 
QuestionInterim Keys longer than 32 bits? Pinmemberchristop_christop3-Jul-13 12:33 
Questiondiffie_hillman PinmemberMember 990145111-Mar-13 6:50 
GeneralMy vote of 5 PinmemberMember 43208448-Jun-12 12:22 
Questionhow to run Pinmembermohdamirr12-Feb-12 7:58 
GeneralInformation about Diffie hellman Pinmembersudhakatr2-Feb-10 22:54 
GeneralRe: Information about Diffie hellman PinmemberGriffter UK2-Feb-10 23:00 
Generalerror setting up the demo Pinmemberswati chaudhari25-Oct-09 18:23 
GeneralRe: error setting up the demo PinmemberSnehlata Shaw17-Nov-09 10:16 
GeneralRe: error setting up the demo PinmemberGriffter UK17-Nov-09 23:54 
GeneralLicense of this code PinmemberJonh Wendell13-Nov-08 4:35 
GeneralRe: License of this code PinmemberGriffter UK13-Nov-08 6:32 
QuestionErrors Pinmemberzeeshan5114-Oct-08 23:14 
AnswerRe: Errors PinmemberGriffter UK13-Nov-08 6:16 
Generalminor changes PinmemberMember 31219318-Oct-08 11:37 
GeneralCompile on linux PinmemberFilipe Niero Felisbino2-Jun-08 9:33 
GeneralRe: Compile on linux PinmemberMrLeeGriffiths3-Jun-08 0:21 
QuestionProblem compiling Pinmemberwygno29-Apr-07 18:58 
AnswerRe: Problem compiling PinmemberMrLeeGriffiths9-Apr-07 23:51 
QuestionEnquiry Pinmemberwygno28-Apr-07 1:46 
AnswerRe: Enquiry PinmemberMrLeeGriffiths9-Apr-07 23:49 
GeneralDiffie Hellman Key exchange PinmemberNaeem Qazi21-Nov-06 10:44 
GeneralRe: Diffie Hellman Key exchange PinmemberGarth J Lancaster21-Nov-06 11:23 
GeneralRe: Diffie Hellman Key exchange PinmemberNaeem Qazi22-Nov-06 0:29 
GeneralRe: Diffie Hellman Key exchange PinmemberHashbullet30-Mar-07 2:09 
QuestionDiffie Hellman Key exchange Pinmembergokikrishnan26-Apr-07 3:45 
GeneralGUID problem FIXED PinmemberMrLeeGriffiths6-Jul-06 7:23 
GeneralRe: GUID problem FIXED Pinmemberqaziejaz8-Oct-07 23:36 
GeneralMagetting errors coz of GUID Pinsussvirgo_aquarian14-Oct-05 16:44 
GeneralDH 1024 bits PinmemberY G14-Sep-05 11:39 
GeneralRe: DH 1024 bits PinmemberHashbullet30-Mar-07 2:07 
QuestionLimitations? Pinmembergthakral10-Aug-05 10:54 
AnswerRe: Limitations? PinmemberMrLeeGriffiths10-Aug-05 23:17 
Generalruntime optimization Pinmemberhaneen28-Jul-05 6:55 
GeneralRe: Generating Primes, DES and a whole lot more! PinmemberMrLeeGriffiths29-Jul-05 5:12 
QuestionRe: Generating Primes, DES and a whole lot more! Pinmembernoooomen31-Oct-06 11:57 
Generalpublic key cryptography PinsussAnonymous16-Apr-05 10:21 
GeneralRe: public key cryptography PinmemberMrLeeGriffiths17-Apr-05 22:57 
Generalquestion PinmemberOmar manama6-Apr-05 12:40 
GeneralHelp required regarding IKE Pinmemberdendude21-Nov-04 20:58 
GeneralCoding Language Pinmemberarchanagoel11-May-04 23:30 
QuestionWhat encryption library ? Pinmemberpb949413-Jan-04 22:02 
AnswerRe: What encryption library ? PinmemberMrLeeGriffiths13-Jan-04 23:12 
GeneralRe: What encryption library ? Pinmemberpb949414-Jan-04 2:56 
GeneralRe: What encryption library ? PinmemberMrLeeGriffiths14-Jan-04 3:33 
GeneralRe: What encryption library ? Pinmemberpb949414-Jan-04 4:28 
GeneralRe: What encryption library ? PinmemberMrLeeGriffiths14-Jan-04 4:44 
GeneralRe: What encryption library ? Pinmembercaractacus15-Jan-04 0:52 
QuestionWhat of small subgroup attacks? PinmemberAdam Nelson9-Jan-04 7:03 
AnswerRe: What of small subgroup attacks? PinmemberMrLeeGriffiths11-Jan-04 0:29 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 14 Nov 2008
Article Copyright 2004 by Griffter UK
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid