txlty
2014-03-29 22:30:36 +08:00
五年前,从php 4开始就自带了。而且跨平台没问题。
linux下默认支持。win下,编辑php.ini把extension=php_openssl.dll 打开。
测试代码:
<?php
function pubEncrypt($data,$pubKey){ //公钥加密
$pubKey = openssl_pkey_get_public($pubKey);
openssl_public_encrypt($data, $encrypted, $pubKey);
return base64_encode($encrypted);
}
function privDecrypt($data,$privKey){ //私钥解密
$privKey=openssl_pkey_get_private($privKey);
$encrypted = base64_decode($data);
openssl_private_decrypt($encrypted, $decrypted, $privKey);
return $decrypted;
}
$pubKey="-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANSlDRoxEXWQrbIbFig3SuT/YH/K0q5/
OLBVloeXSr+dc+PWvXsRnwkxIkX5KznPCG9zLGe/Ga9XRyp+k2YrnrsCAwEAAQ==
-----END PUBLIC KEY-----"; //公钥
$privKey="-----BEGIN RSA PRIVATE KEY-----
MIIBOQIBAAJBANSlDRoxEXWQrbIbFig3SuT/YH/K0q5/OLBVloeXSr+dc+PWvXsR
nwkxIkX5KznPCG9zLGe/Ga9XRyp+k2YrnrsCAwEAAQJACg70m7SOet0pGi+l0Kks
5cAetGzHonFqIQ0rXaaOZpnZ7VeBTjklFVlVv/6UGr0h7jLclU+0UcEZ1lxwQUBx
wQIhAPoD6k+WOJld9D0p6ECLlS0ii4spj/T5o5ODdI+pkS2JAiEA2bwhZICOwb8w
FjfJOoKFcfuUfTk0n8y1DokfRQBC/SMCIBoZ8opgioQodOk1yNOEPwy5QIxEyIyw
y00JOlANSeTRAiBm+oeAlRzvnZEMsQ3HUnvy0N4F68xr19G6MjhL1jfYqQIgGiYa
RlI1jeOWFLKyxOFcNim8HUhfqIebm8Mccc0/3kQ=
-----END RSA PRIVATE KEY-----"; //私钥
$string='一二三四五六七八九十';
echo '公钥加密:';
$en = pubEncrypt($string,$pubKey);
echo $en . '<br /> 私钥解密:';
$de = privDecrypt($en,$privKey);
echo $de;
?>
毫无问题~