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);
DH->CreateSenderEncryptionKey(key,RInterim);
The receiving application does the following:
__int64 n = 0;
__int64 g = 0;
__int64 SInterim = 0;
__int64 RInterim = 0;
__int64 key = 0;
DH->CreateRecipientInterKey(RInterim);
DH->CreateRecipientEncryptionKey(key,SInterim);
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.