PHP
·
发表于 5年以前
·
阅读量:8299
µÏÆ£ºÕ¶uÂu(Diffie¨CHellman)ÊÇÒ»¸o¿ÉÒÔÈÃË«½ÔÚ²»°²È«µÄ¹«¹²ÐŵÀÉϽ¨Á¢ÃØÔ¿µÄÒ»ÖÖËa¨£¬Ë«½ºoÆھͿÉÒÔÀuÓÃÕa¸oÃØÔ¿¼ÓÃÜ(ÈçRC4)ÄÚÈÝ¡£
µÏÆ£ºÕ¶uÂu(Diffie¨CHellman)Ëa¨ÔÀiºÜ¼oµ¥£º
ÈçÉÏÔÀi£¬×iºoºÜÈÝÒ×ͨ¹ýÊýѧÔÀiÖ¤Ã÷(g^b%p)^a%p = (g^a%p)^b%p£¬Òo´ËËuÃǵõ½Ò»¸oÏaͬµÄÃÜÔ¿¡£
ÉÏÃae³ýÁËa,bºÍ×iºoµÃ³oµÄ¹«¹²ÃÜÔ¿ÊÇÃØÃܵģ¬ÆaËu¶¼ÊÇ¿ÉÒÔÔÚ¹«¹²ÐŵÀÉÏ´«µÝ¡£Êµ¼ÊÔËÓÃÖÐpºÜ´o(300λÒÔÉÏ)£¬gͨ³£È¡2»o5¡£ÄÇô¼¸ºo²»¿ÉÄÜ´Óp,gºÍg^a%pËa³oa(ÀeÉ¢ÊýѧÎÊÌa)¡£
ºÜ¶aÓiÑÔ¶¼¶Ô¸ÃËa*¨×oÁËʵÏÖ£¬ÒÔPHP packageÖÐCrypt_DiffieHellmanΪÀý£º
<?php
include 'DiffieHellman.php';
/*
* Alice: prime = 563
* generator = 5
* private key = 9
* Bob: prime = 563
* generator = 5
* private key = 14
*/
$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