首頁 > 後端開發 > php教程 > php openssl rsa 加密解密

php openssl rsa 加密解密

WBOY
發布: 2016-08-08 09:24:21
原創
1068 人瀏覽過

$config = array(
//"config" =>"D:/phpserver/Lighttpd/openssl.cnf",
//'config' =>'D:/phpStudy/Lighttpd/ OpenSSL.cnf ',
'private_key_bits' => 1024,    // 密鑰大小。
'private_key_type' => OPENSSL_KEYTYPE_RSA
);

//$res = openssl_pkey_new();
$res = openssl_pkey_new($config );

/ / 取得私鑰
// openssl_pkey_export($res, $privkey, "PassPhrase number 1" );
 openssl_pkey_export($res, $privkey);
var_dump($privkey); openssl_pkey_get_details ($res);

 

// echo "------------>
gt;";
// print_r($pubkey["rsa" ]);
// $ bin_str=$pubkey["rsa"]["n"];
// print_r($bin_str);
// echo "
";
// // echo $bin_hex_str = pack("H* " , bin2hex($bin_str));
// echo $bin_hex_str =  bin2hex($bin_str);
// echo "
--------- ---";

 $pubkey=$pubkey["key"];
// var_dump($privkey);
// var_dump($pubkey);
 echo $privkey。 pubkey 。 fQwM7akI aPpzU0+kkWjTSXiDqFUv5y2vaA3W2Tre5N51uCwUK6/ LfzHX
Bg0d+Bnlzy6Kb6bpgPgvbhAkNbvjbCS0V9h4v3WUTLR9QauJ05Q8qe70sQIDAQAB
AoGAb/9aZ1MMkNImfbIilXk1AvVn353qy51yNyTxFfhbyISIKVSYqtuSUYfFbJh7
eZYz8UWs1jxuqB847IUZ/eJZzpVev480Tv9YejN0DhJ4Yw6y/kpJWo/9kEcET6Tk
vatXh+wgmieszkxe0kUvk41GPV7a4GWWQj6P4vjx+qktaNECQQDuo7ENW10dZXLG
YNKqUhgkdi9NPjRlHCgZ4RW0uoRqUsggqNVkaqpY8BaJOkkXWiQ0aVTPBiwo8k0+

S50rGNYNAkEA3DgxY9OwRc65VqS /UxV28pyLe2gy0XSVWA97SVNvYJJ58iNGEVYs

vEZ3GrTNAZpbPKcruCS6NiDK761Ot5A0NQJAOsRO1pY4QQP5lsbDOjRLQ7djAKEf
6YJckbrvSHliIHjQVdKd8kOePZaqRhL3auvE3K 9Ptkez2kWJJh0i0DkmhQJBAMCm
F+7wnWA0PEbj0MvqFyiechb/TJWXm/lskbnP17tv6GY/HWoxGWmLa15dlIo2cmJZ
DBIHby8y+NU7k4VaDZECQQCO018O5TJWl+O+ bWyzV1eR60 3YP+JVjiSKUPtuyzVS
NIz+ClwspGmdUwPWlLqr5JfKm+OfpaLIepfJinsCgwpP
-----結束//MIICXAIBAAKBgQDVFOCE2nEBi+4FYvS36j1ShHHvusR0dXXe9NzAA/biH6NODywH
//cQo2Lyco6oSvY+cSsPewjjyatRCt/Ri6c/am6 4C1+/amHMs/i15c/am PHDxYKAq/hW9S1NeYr2x4eJdpYNYucLn8XWgeLRnlJUnBl/薩奇達卡
//AoGAK2oz28hKKB+DorNT2zktv2de85JhPr1EA+ZuJ+1m2xHJKzlJiYinYiY 9Wp1KiZPEforRWzz1wmuj8koq95vcxBeuyjX4ez4Dio 6
//Vmr9BABFuoAh0GAq8kTJOz0DURCEcwv+iI7ILaNoRjPFWuUCQQDveqRLE2/N8NIy
//P7YVnniAiaSL/e+XYQXCMIDsdEdqZNRjhjfDoBqpV/tGjnuAn9zBVHNNLbLni5nf
//2n qgrh5jAkEA48gQmBO9koOnvO//Q581JSBKfga4i7DetQ80OD4kYt6fwezkaeHX
//k64sjmpfD/Ys2heKFhyNDO/k0NsUxGlNwwJBAKPx7 /u9qLLyBNSFrWALhy9oC5AT
//Hi0iLNEOZ5 R43lNGB+jCf/aa6uW357wb2wzpZDWuXOkfOfUuKv+HEKBj00UCQCxd
/ /woWsiqcoB5rwcBrevr49fOTxMC7dQ3Omw3gU +odbb2iZWKAb Ah9ctIcExwFX5MKS
//BS5rXzne3AWmNnRPQnsCQDdOCBxkF5YYHctHqmDun​​pkp5p5p5p lPIJ+TfxxFIqesHdph4s/lw6T2ISY=
//-----END RSA PRIV吃鑰匙-----';

$public_key = '-----開始公鑰-----

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNSQ4AmPZE01MpeO9dabRDsS0X
wxKekEFUoWJneFG+NA6P52JsISw79QYNfQM Tre5N 51uCwUK6/LfzHXBg0d+Bnlzy6Kb6bpgPgvbhAkNbvjbCS0V9h4v3WU
TLR9QauJ05Q8qe70sQIDAQAB
-----結public_key=$pubkey;

$fp=fopen("private_key","w");
fwrite($fp,$private_key);;
fclose($fp);

$fp=fopen( w");
fwrite($fp,$public_key);
fclose($fp);

//回顯$private_key;
$pi_key =  openssl_pkey_get_private( $private_key);可用的,可用回傳資源id 資源id

$pu_key = openssl_pkey_get_public($public_key);//該函數可用來判斷私鑰是否可用的

print_r( $pi_key);echo "n";
print_r($pu_key); echo "n";
echo "
-------------------- --
";

$data = "woshizhu"; / / 原始資料
$encrypted = "";

$已解密=“”;


echo "來源資料:",$data,"
";

echo "私鑰加密:n";

openssl_private_encrypt($data,$encrypted,$pi_key);//私鑰加密
$encrypted = base64_encode($encrypted);//加密後的內容通常含有特殊字符,需要編碼轉換下,在網路間透過url時要注意base64編碼是否是url安全的
echo "
----------私鑰加密------------
";
echo $encrypted,"
";
echo "
----------私鑰加密------------
";
echo "public key decrypt:n";
$decrypted="";
openssl_public_decrypt(base64_decode($encrypted),$decrypted,$pu_key);//私鑰加密的內容透過公鑰可用來解密
echo $decrypted," n";

echo "---------------------------------------n";
echo "public key encrypt:n";

openssl_public_encrypt($data,$encrypted,$pu_key);//公鑰加密
$encrypted = base64_encode($encrypted);
decrypted="";

//$encrypted="JBeapcp9iWWYJYElgqtrZxfxM4wVkCaSn/oJZ7NjfR23o76fdbxEXpf+PGWACw3PeTdObwL4108wR36fdbxEXpf+PGWACw3PeTdObwL4108wR3ihKmyiY 5XxHWQc3Dhbq16JWHYZ2d+RrOpHd4x84GF3JXwivrGO4=";

echo "private key decrypt:n";
openssl_private_decrypt(base64_decode($encrypdecode($encrted),$depi);私鑰解密
echo "--->:".$decrypted,"n";
?>

以上就介紹了php openssl rsa 加密解密,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板