PHP RSA 加密 验签
1. 先把pfx转成pem,openssl命令如下:
openssl pkcs12 -in KEYSTORE.pfx -out KEYSTORE.pem -nodes
常用openssl的命令可参考这里,建议大家把down下来保存。
2. 使用私钥加密,如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
$priKeyConten= file_get_contents("abc.pem");
$priKey = openssl_get_privatekey($priKeyConten);
error_log("私钥=".$priKey."\n", 3, $file_name);
openssl_private_encrypt(md5($plain_text,TRUE), $crypted, $priKey);
$encryptDate =base64_encode($crypted);
error_log("加密后结果=".$encryptDate."\n", 3, $file_name);
3. 公钥验签,如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
$key_content = file_get_contents("public.pem");
$pubkeyid = openssl_get_publickey($key_content);
$sign_bin = base64_decode($sign);
openssl_public_decrypt($sig_bin, $out, $pubkeyid);
//再将按验签规则生成的明文做md5与$out比较是否相同,相同则验签OK。
加密 解密 签名 验签 这些道道还没弄得清楚,以上内容写得乱七八糟,囫囵吞枣,不三不四,得专门研究研究"-_-"...
PS:openssl将pfx转cer, 参考这里
1. pkcs12 -in abc.pfx -out abc.crt -nokeys -clcerts
enter import password.
2. x509 -inform pem -in abc.crt -outform der -out abc.cer
the abc.cer is ok.