Diffie-Hellman是一种允许双方在不安全的公共信道上建立密钥的算法,双方可以在后期使用该密钥加密(如RC4)内容。Diffie-Hellman算法的原理很简单:
根据上述原理,很容易用数学原理证明(g b% p) a% p=(g a% p) b% p,于是他们得到了同一个密钥。除了a、b和最终获得的公钥是秘密的,其他都可以在公共信道上传输。实际上,p非常大(超过300位数),g通常是2或5。那么从p,g和g a% p计算出一个(离散数学问题)几乎是不可能的。
很多语言都实现了该算法,以PHP包中的Crypt _ DiffieHellman为例:
?phpinclude ' DiffieHellman.php/* * Alice: prime=563 *生成器=5 *私钥=9 * Bob: prime=563 *生成器=5 *私钥=14 */$ p=563;$ g=5;$ Alice=new Crypt _ Diffihellman($ p,$g,9);$ Alice _ PubKey=$ Alice-generateKeys()-GetPubKey();$ bob=new Crypt _ diffihellman($ p,$g,14);$ bob _ PubKey=$ bob-generateKeys()-GetPubKey();$ Alice _ computeKey=$ Alice-computesecretekey($ bob _ pubKey)-getSharedSecretKey();$ bob _ computeKey=$ bob-computesecretekey($ Alice _ pubKey)-getSharedSecretKey();echo“{ $ Alice _ PubKey }-{ $ bob _ PubKey }-{ $ Alice _ ComputeKey }-{ $ bob _ ComputeKey } ';//78-534-117-117