_contents('./rsa_public_key.pem'); $publicKey = openssl_pkey_get_public($publicKey);
return (openssl_public_decrypt(base64_decode($str), $decryptedStr, $publicKey)) ? $decryptedStr : null; } }
/** * 公钥加密/私钥解密 * * @param $str string 待加解密字符串 * @param $isEncrypt boolean * return string|null */ function opensslPublicEncrypt($str , $isEncrypt = true) {
if($isEncrypt){ $publicKey = file_get_contents('./rsa_public_key.pem'); $publicKey = openssl_pkey_get_public($publicKey);
return openssl_public_encrypt($str, $encryptedStr, $publicKey) ? base64_encode($encryptedStr) : null; }else{ $privateKey = file_get_contents('./rsa_private_key.pem'); $privateKey = openssl_pkey_get_private($privateKey);
return (openssl_private_decrypt(base64_decode($str), $decryptedStr, $privateKey)) ? $decryptedStr : null; } }
$config = array(); $config['title'] = 'PHP is the best program language.'; $config['comment'] = 'PHPERS are great people.';
$privateEncrypt = opensslPrivateEncrypt(json_encode($config)); echo("##privateEncrypt is:" . PHP_EOL); echo($privateEncrypt . PHP_EOL); $privateDecrypt = opensslPrivateEncrypt($privateEncrypt, false); echo("##privateDecrypt is:" . PHP_EOL); echo($privateDecrypt . PHP_EOL);
$publicEncrypt = opensslPublicEncrypt(json_encode($config)); echo("##publicEncrypt is:" . PHP_EOL); echo($publicEncrypt . PHP_EOL); $publicDecrypt = opensslPublicEncrypt($publicEncrypt, false); echo("##publicDecrypt is:" . PHP_EOL); echo($publicDecrypt . PHP_EOL);
##执行脚本 [root@iz2vcf47jzvf8dxrapolf7z test]# php openssl.php ##privateEncrypt is: mLrLIAwbwlE69Yj5/lnNw1t8qSjhnFa+96s/kSMYweAn/HEsV7jfVAJ6mn/FY2DRRWkKeOnguUYsRcFTBcS1ieG7UtqbUAASXA5dwVgtTrFoDcDhHMl7p90+dIO8n+vMoBx1kkUegpvtH03y3MgUVSj/BLkLE8jrFXyjGufIcv0= ##privateDecrypt is: {"title":"PHP is the best program language.","comment":"PHPERS are great people."} ##publicEncrypt is: NW2k5m2pKrZmEMSHXiK7mfyC+yDjH1+b6TrEMPv+ywBfsUlo2P8eWwcXOQxvsV4UG87a1S4Xa2QySntdEwhpYoim97457ODVVCb6jx+cqqdWJ1wlLS+gx7FJxw7Z0kMPmCm5iMcQwWPK+UzF+dpc/gJFa9uGAAmYczUumOauAx0= ##publicDecrypt is: {"title":"PHP is the best program language.","comment":"PHPERS are great people."}
pkcs标准:
PKCS标准汇总 |
|
版本 |
名称 |
简介 |
PKCS #1 |
2.1 |
RSA密码编译标准(RSA Cryptography Standard) |
定义了RSA的数理基础、公/私钥格式,以及加/解密、签/验章的流程。1.5版本曾经遭到攻击。 |
PKCS #2 |
- |
撤销 |
原本是用以规范RSA加密摘要的转换方式,现已被纳入PKCS#1之中。 |
PKCS #3 |
1.4 |
DH密钥协议标准(Diffie-Hellman key agreement Standard) |
规范以DH密钥协议为基础的密钥协议标准。其功能,可以让两方通过金议协议,拟定一把会议密钥(Session key)。 |
PKCS #4 |
- |
撤销 |
原本用以规范转换RSA密钥的流程。已被纳入PKCS#1之中。 |
PKCS #5 |
2.0 |
密码基植加密标准(Password-based Encryption Standard) |
参见RFC 2898与PBKDF2。 |
PKCS #6 |
1.5 |
证书扩展语法标准(Extended-Certificate Syntax Standard) |
将原本X.509的证书格式标准加以扩充。 |
PKCS #7 |
1.5 |
密码消息语法标准(Cryptographic Message Syntax Standard) |
参见RFC 2315。规范了以公开密钥基础设施(PKI)所产生之签名/密文之格式。其目的一样是为了拓展数字证书的应用。其中,包含了S/MIME与CMS。 |
PKCS #8 |
1.2 |
私钥消息表示标准(Private-Key Information Syntax Standard). |
Apache读取证书私钥的标准。 |
PKCS #9 |
2.0 |
选择属性格式(Selected Attribute Types) |
定义PKCS#6、7、8、10的选择属性格式。 |
PKCS #10 |
1.7 |
证书申请标准(Certification Request Standard) |
参见RFC 2986。规范了向证书中心申请证书之CSR(certificate signing requ |
|