This article mainly introduces the Diffie–hellman-Herman Key Exchange (Diffie–hellman) algorithm principle and PHP implementation version, the need for friends can refer to the
Diffie–hellman-Herman (Diffie–hellman) is an algorithm that allows both parties to create secret keys on insecure public channels, which can be used to encrypt (such as RC4) content later in the two parties.
The Diffie–hellman-Herman (Diffie–hellman) algorithm is simple:
As above principle, it is easy to prove by mathematical principle (g^b%p) ^a%p = (g^a%p) ^b%p, so they get a same key.
In addition to the A,B and the final public key is secret, the others can be passed on the public channel. The actual use of P is very large (more than 300 digits), G usually take 2 or 5. It is almost impossible to calculate a (discrete mathematical problem) from P,g and g^a%p.
Many languages have implemented this algorithm, with PHP package Crypt_diffiehellman as an example:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php include ' diffiehellman.php '; * * Alice:prime = 563 * Generator = 5 * Private key = 9 * Bob:prime = 563 * Generator = 5 * Private key = * * $p = 563; $g = 5; $alice = new Crypt_diffiehellman ($p, $g, 9); $alice _pubkey = $alice->generatekeys ()->getpublickey (); $bob = new Crypt_diffiehellman ($p, $g, 14); $bob _pubkey = $bob->generatekeys ()->getpublickey (); $alice _computekey = $alice->computesecretkey ($bob _pubkey)->getsharedsecretkey (); $bob _computekey = $bob->computesecretkey ($alice _pubkey)->getsharedsecretkey (); echo "{$alice _pubkey}-{$bob _pubkey}-{$alice _computekey}-{$bob _computekey}"; 78-534-117-117 |