5,426,531 members and growing! (15,313 online)
Email Password   helpLost your password?
General Programming » Cryptography & Security » Cryptography     Intermediate

Diffie-Hellman Key Exchange Example

By Griffter UK

An example of how an encryption key can be shared by two users using the Diffie-Hellman key exchange approach.
VC7.1, C++Windows, Win2K, WinXP, Win2003, MFC, VS.NET2003, Visual Studio, Dev

Posted: 8 Jan 2004
Updated: 6 Jul 2006
Views: 82,177
Bookmarked: 27 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
20 votes for this Article.
Popularity: 4.42 Rating: 3.40 out of 5
3 votes, 15.0%
1
0 votes, 0.0%
2
2 votes, 10.0%
3
2 votes, 10.0%
4
13 votes, 65.0%
5

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" text box on the sender application. Both applications should then be able to generate the same key by clicking "Generate Key".

Using the Source Code

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

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

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 CDRMCrypto 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Griffter UK



Occupation: Software Developer (Senior)
Location: United Kingdom United Kingdom

Other popular Cryptography & Security articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralCompile on linuxmemberFilipe Niero Felisbino9:33 2 Jun '08  
GeneralRe: Compile on linuxmemberMrLeeGriffiths0:21 3 Jun '08  
QuestionProblem compilingmemberwygno218:58 9 Apr '07  
AnswerRe: Problem compilingmemberMrLeeGriffiths23:51 9 Apr '07  
QuestionEnquirymemberwygno21:46 8 Apr '07  
AnswerRe: EnquirymemberMrLeeGriffiths23:49 9 Apr '07  
GeneralDiffie Hellman Key exchangememberNaeem Qazi10:44 21 Nov '06  
GeneralRe: Diffie Hellman Key exchangememberGarth J Lancaster11:23 21 Nov '06  
GeneralRe: Diffie Hellman Key exchangememberNaeem Qazi0:29 22 Nov '06  
GeneralRe: Diffie Hellman Key exchangememberHashbullet2:09 30 Mar '07  
QuestionDiffie Hellman Key exchangemembergokikrishnan3:45 26 Apr '07  
GeneralGUID problem FIXEDmemberMrLeeGriffiths7:23 6 Jul '06  
GeneralRe: GUID problem FIXEDmemberqaziejaz23:36 8 Oct '07  
GeneralMagetting errors coz of GUIDsussvirgo_aquarian16:44 14 Oct '05  
GeneralDH 1024 bitsmemberY G11:39 14 Sep '05  
GeneralRe: DH 1024 bitsmemberHashbullet2:07 30 Mar '07  
GeneralLimitations?membergthakral10:54 10 Aug '05  
GeneralRe: Limitations?memberMrLeeGriffiths23:17 10 Aug '05  
Generalruntime optimizationmemberhaneen6:55 28 Jul '05  
GeneralRe: Generating Primes, DES and a whole lot more!memberMrLeeGriffiths5:12 29 Jul '05  
QuestionRe: Generating Primes, DES and a whole lot more!membernoooomen11:57 31 Oct '06  
Generalpublic key cryptographysussAnonymous10:21 16 Apr '05  
GeneralRe: public key cryptographymemberMrLeeGriffiths22:57 17 Apr '05  
GeneralquestionmemberOmar manama12:40 6 Apr '05  
GeneralHelp required regarding IKEmemberdendude20:58 21 Nov '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Jul 2006
Editor: Smitha Vijayan
Copyright 2004 by Griffter UK
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project