The Diffie–Hellman key exchange method allows two parties that have no prior knowledge of each other to jointly establish a shared secret key over an insecure channel.
This key can then be used to encrypt subsequent communications using a symmetric key cipher.
The simplest and the original implementation of the protocol uses the multiplicative group of integers modulo p, where p is prime, and g is a primitive root modulo p.
3^2 mod 2.
2^5 mod 7 = 4.
The D-H key is g^(ab) mod p.
I recommend see wikipedia,very good and exhaustive explanation: