本源企业项目需要对接第三方,难免涉及到一些加解密。作为外包公司的一员,这种情况就更常见了。中间遇到过几次RSA加密,这次遇到了.pfx私钥证书加密和.cer公钥证书解密的功能。这是一个摘要。.pfx私钥签名$toSign="helloworld";//要加密的字符串$psd='zsmm';//证书私钥密码$certs=array();$cert_path="c:/cert/test.pfx";//证书的绝对地址openssl_pkcs12_read(file_get_contents($cert_path),$certs,$psd);if(!$certs){exit('获取秘钥失败!');}if(openssl_sign($toSign),$binarySignature,$certs['pkey'])){echobase64_encode($binarySignature);//加密结果为二进制,需要进行base64编码}.cer公钥验证。cer证书不需要使用opensslx509-informder-inpub.cer-outpub.pem命令转换证书格式。$data="你好世界";//签名前的原始文本$sign="binarySignature";//签名$cert_path="c:/cert/test.cer'";//证书的绝对地址$source=openssl_pkey_get_public(file_get_contents($cert_path));echoopenssl_verify($data,$sign,$source);//1:签名验证通过,0:签名验证失败,-1:系统内部错误
