You are on page 1of 2

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: Collapse | Copy Code
__int64 __int64 __int64 __int64 __int64 n = 0; g = 0; SInterim = 0; RInterim = 0; 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:

Collapse | Copy Code


__int64 __int64 __int64 __int64 __int64 n = 0; g = 0; SInterim = 0; RInterim = 0; 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 Software Developer (Senior) United Kingdom

You might also like