First, the Diffie Hellman (DH) Key Exchange is a cryptographic method of sharing a secret key over a public or insecure network, this key can be used to encrypt and decrypt data using symmetric key ciphers such as Blowfish as used by CO

In this guide I'll cover the basic concept on how DH works and how to generate keys.

I'll be be using two hosts/computers for my explanation, I'll refer to them as PC1 and PC2.

Now, both PC1 and PC2 need to agree on a Prime number (P) greater than 2 and an Integer (G) which is less than P. PC1 generates the P and G and sends them over to PC2, it's not a big deal if a spy or hacker gains access to the P and G, since that's the whole idea of of using DH in an insecure network. Next both PC's generate a random secret key (let's call this X) which will never be sent over the network, X also has to be less than P-1, now let's apply what we did now.

PC1 Generates P=41, G=35 and sends them over to PC2, both now have the same P and G, PC1 generates X=9, PC generates X=24, so that means:

Code:

PC1: PC2:
P=41 P=41
G=35 G=35
X=9 X=24

Now we start getting our public keys and secret keys (that's different than random secret key X), first on PC1 we get our integer and raise it to the power of our X then we mod (modulus) by our prime number, (G^X) % P, that's now our public key, we send that public key over to PC2, now PC2 takes that public key and raises it to the power of it's own X then mod by P again, that's our Secret Key of PC2, formula (PC1_PubKey^X) % P, now we also do the same process on PC2 by generating the PC2 pub key using the same formula (G^X) % P and send it to PC1 and do the same and get the PC1 Secret Key using the formula (PC2_PubKey^X) % P, and that turns out to be the same public key as the one on PC2 without actually sending it over the network. Now even though this information was sent over a public/insecure network, the random secrets (X) were never sent over the network and that makes it extremely difficult to reverse these numbers. Now let's use my examples above.

Code:

PC1: PC2:
P=41 P=41
G=35 G=35
X=9 X=24

PC1: (35^9) % 41 = 22, set that as public key and send over to PC2

PC2: (22^24) % 41 = 18, that's our Secret Key, now we generate a public key, (35^24) % 41 = 16, send it to PC1

PC1: (16^9) % 41 = 18, that's the same secret key as PC2

Code:

PC1: PC2:
P=41 P=41
G=35 G=35
X=9 X=24
Step 1: 35^9 % 41 = 22 (Sent to PC2) Step 2: 22^24 % 41 = 18 (Not Sent)
Step 4: 16^9 % 41 = 18 (Not Sent) Step 3: 35^24 % 41 = 16 (Sent to PC1)
PubKey = 22 PubKey = 16
SecretKey = 18 SecretKey = 18

Hope you like it and don't forget a +Thanks