Click here to Skip to main content
6,634,665 members and growing! (16,503 online)
Email Password   helpLost your password?
General Programming » Cryptography & Security » Cryptography     Intermediate License: The Code Project Open License (CPOL)

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.1Win2K, WinXP, Win2003, MFC, VS.NET2003, Dev
Posted:8 Jan 2004
Updated:14 Nov 2008
Views:112,118
Bookmarked:43 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
25 votes for this article.
Popularity: 4.82 Rating: 3.45 out of 5
3 votes, 12.0%
1
1 vote, 4.0%
2
4 votes, 16.0%
3
2 votes, 8.0%
4
15 votes, 60.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" 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)

About the Author

Griffter UK


Member

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

Other popular Cryptography & Security articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 52 (Total in Forum: 52) (Refresh)FirstPrevNext
Generalerror setting up the demo Pinmemberswati chaudhari18:23 25 Oct '09  
GeneralRe: error setting up the demo PinmemberSnehlata Shaw10:16 17 Nov '09  
GeneralRe: error setting up the demo PinmemberGriffter UK23:54 17 Nov '09  
GeneralLicense of this code PinmemberJonh Wendell4:35 13 Nov '08  
GeneralRe: License of this code PinmemberGriffter UK6:32 13 Nov '08  
QuestionErrors Pinmemberzeeshan5123:14 14 Oct '08  
AnswerRe: Errors PinmemberGriffter UK6:16 13 Nov '08  
Generalminor changes PinmemberMember 312193111:37 8 Oct '08  
GeneralCompile on linux PinmemberFilipe Niero Felisbino9:33 2 Jun '08  
GeneralRe: Compile on linux PinmemberMrLeeGriffiths0:21 3 Jun '08  
QuestionProblem compiling Pinmemberwygno218:58 9 Apr '07  
AnswerRe: Problem compiling PinmemberMrLeeGriffiths23:51 9 Apr '07  
QuestionEnquiry Pinmemberwygno21:46 8 Apr '07  
AnswerRe: Enquiry PinmemberMrLeeGriffiths23:49 9 Apr '07  
GeneralDiffie Hellman Key exchange PinmemberNaeem Qazi10:44 21 Nov '06  
GeneralRe: Diffie Hellman Key exchange PinmemberGarth J Lancaster11:23 21 Nov '06  
GeneralRe: Diffie Hellman Key exchange PinmemberNaeem Qazi0:29 22 Nov '06  
GeneralRe: Diffie Hellman Key exchange PinmemberHashbullet2:09 30 Mar '07  
QuestionDiffie Hellman Key exchange Pinmembergokikrishnan3:45 26 Apr '07  
GeneralGUID problem FIXED PinmemberMrLeeGriffiths7:23 6 Jul '06  
GeneralRe: GUID problem FIXED Pinmemberqaziejaz23:36 8 Oct '07  
GeneralMagetting errors coz of GUID Pinsussvirgo_aquarian16:44 14 Oct '05  
GeneralDH 1024 bits PinmemberY G11:39 14 Sep '05  
GeneralRe: DH 1024 bits PinmemberHashbullet2:07 30 Mar '07  
GeneralLimitations? Pinmembergthakral10:54 10 Aug '05  

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

PermaLink | Privacy | Terms of Use
Last Updated: 14 Nov 2008
Editor: Sean Ewington
Copyright 2004 by Griffter UK
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project