The first published key enchange algorithm, based on the discrete logarithm problem.

There is a universal small generator prime g, and a universal large prime modulus m.

The private key p is randomly chosen as a integer less than m. The public key is derived by computing g^p (mod m). Given a public key q, the common key is computed with q^p (mod m).

The patent (U.S. 4,200,770) was owned by Public Key Partners. It expired on 9/6/1997.

The following interesting explanation of a Diffie-Hellman key exchange, including a 2-line (!) Perl script to implement it, is from:

It is by Adam Back. (Munged email: <not eve but her partner ____> at cypherspace dot org).
Please note that I had to fudge a lot of things in it (some <pre> tags wanted to treat < as opening an HTML tag, and some didn't.) So if there's anything weird, lemme' know. (Unless I've just noded this...I have a sneaking suspicion that the Scratch Pad isn't quite which case, I am already busily working to fix it!).


Diffie-Hellman in 2 lines of perl

#!/usr/local/bin/perl -- -export-a-crypto-system-sig Diffie-Hellman-2-lines
($g,$e,$m)=@ARGV,$m||die"$0 gen exp mod\n";print`echo "16dio1[d2%Sa2/d0<X+d

Diffie-Hellman key exchange allows two parties who have not met to exchange keys securely on an unsecure communication path. Typically D-H is used to exchange a randomly generated conventional encryption key, the rest of the exchange is then encrypted with the conventional cipher. It has been used with DES, 3DES, IDEA, RC4 though basically the approach of using D-H key exchange can be used for any conventional stream or block cipher.

PGP itself operates in a similar fashion, except that PGP uses RSA for key exchange, and IDEA as the conventional cipher.

The maths for Diffie-Hellman is quite simple.

Here is an example:

We are trying to exchange a random key for communication. Say that we will be using the RC4 stream cipher as our conventional cipher. Here's the stages in the process.

  • You advertise your public generator g, and public prime modulus m as your "public key" in the same way that you would advertise an RSA public key, you give this to anyone you wish to exchange messages with. This is no requirement for these numbers to be kept secret, all knowledge of these numbers gives is the ability to send you messages.

    You do not tell anyone what your secret exponent x is.

    You initiate a key exchange by calculating a:

    	a = g  mod m
    and sending me the number a.

    You can use the dh perl program to do this calculation. For the sake of argument we will choose some numbers. Say that your public generator g = 3, your public prime modulus m = 10001 and your secret exponent x = 9a2e (all numbers in hex). Then you would run the perl dh program like this to calculate a:

    % dh g x m
    You send me the following in email:
    g = 3, m = 10001, a = c366
  • Now I choose a session key to use for RC4, using D-H key exchange I can send this to you without an eavesdropper being able to get the key.

    I do not choose a session key directly but rather choose a random number y from which I will calculate the session key.

    Say that the random y = 4c20.

    Now I calculate the session key s:

    	s = a  mod m
    Or using the perl dh program:
    % dh c366 4c20 10001
    So the session key s = ded4

  • In order to send you the session s key I need only calculate:
    	b = g  mod m
    Again using the perl dh program:
    % dh 3 4c20 10001
    So b = 6246

    I send the number b to you in email, now you can recover the session key:

    	s = b  mod m
    So to obtain the session key, using the perl dh program you do:
    % dh 6246 9a2e 10001
    And sure enough out pops the session key as I calculated, s = ded4.

    Now we both have the session key and commence exchanging messages using our agreed key as the key to a symmetric cipher such as rc4.

    So now I can send you (or you can send me) rc4 messages encrypted with our negotiated session key s = ded4:

    % cat msg | rc4 ded4 | uuencode r r | mail 
    A perl (and a C version) of rc4 suitable for the above can be found here. This works because of the mathematical property that:
    	  y           x
    	 x	     y
    	g   mod m = g   mod m
     	     x			y
    	a = g  mod m,      b = g  mod m
    	     y	        x
    =>	s = a  mod m = b  mod m
    x and y never change hands, and yet s has changed hands. Further you can't discover my y and I can't discover your x, and an eavesdropper has neither x nor y and so cannot discover s.

Real example

You can try sending me some RC4 encrypted email, using a D-H negotiated session key.

For security (the above example is for clarity only 32 bit keys are utterly useless for security purposes) we will use 1024 bits. Here is my Diffie-Helman public key, you can have a go at negotiating a D-H key exchange and sending me some RC4 encrypted email.

I have chosen an x (which I won't be telling you this time for obvious reasons), and calculated the corresponding a as described above. Here are my D-H public key numbers:

g = 3

m = 

a =
To send me some mail you will need to generate your choice of random y as described above, and then calculate the session key s:
% dh [a] [y] [m] > s
(where [a] and [m] are the large numbers above cut and pasted in, and [y] is your large random number.) and then calculate key exchange number b:
% dh [g] [y] [m] > b
% mail < b
Then you compose your mail message as file "msg" and rc4 encrypt that using s as calculated above:
% cat msg | rc4 `cat s` | uuencode r r | mail
Comments, html bugs to me [contact info. as at top of node.]


Log in or registerto write something here or to contact authors.