php基于openssl加密解密、验证技巧
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本文实例讲述了PHP实现超简单的SSL加密解密、验证及签名的<span style="color: black;">办法</span>。分享给<span style="color: black;">大众</span>供<span style="color: black;">大众</span>参考,<span style="color: black;">详细</span>如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 sign签名代码:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">function sign($data) {
//读取私钥文件
$priKey = file_get_contents(key/rsa_private_key.pem);
//转换为openssl密钥,<span style="color: black;">必要</span>是<span style="color: black;">无</span>经过pkcs8转换的私钥
$res = openssl_get_privatekey($priKey);
//调用openssl内置签名<span style="color: black;">办法</span>,生成签名$sign
openssl_sign($data, $sign, $res);
//释放资源
openssl_free_key($res);
return $sign;
}</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 verify 验证代码:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">function verify($data, $sign) {
//读取支付宝公钥文件
$pubKey = file_get_contents(key/alipay_public_key.pem);
//转换为openssl格式密钥
$res = openssl_get_publickey($pubKey);
//调用openssl内置<span style="color: black;">办法</span>验签,返回bool值
$result = (bool)openssl_verify($data, $sign, $res);
//释放资源
openssl_free_key($res);
return $result;
}</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. 解密代码</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">function decrypt($content) {
//读取商户私钥
$priKey = file_get_contents(key/rsa_private_key.pem);
//转换为openssl密钥,<span style="color: black;">必要</span>是<span style="color: black;">无</span>经过pkcs8转换的私钥
$res = openssl_get_privatekey($priKey);
//声明明文字符串变量
$result = ;
//循环<span style="color: black;">根据</span>128位解密
for($i = 0; $i < strlen($content)/128; $i++ ) {
$data = substr($content, $i * 128, 128);
//拆<span style="color: black;">掰开</span>长度为128的字符串片段<span style="color: black;">经过</span>私钥进行解密,返回$decrypt解析后的明文
openssl_private_decrypt($data, $decrypt, $res);
//明文片段拼接
$result .= $decrypt;
}
//释放资源
openssl_free_key($res);
//返回明文
return $result;
}</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上内容<span style="color: black;">期盼</span><span style="color: black;">帮忙</span>到<span style="color: black;">大众</span>,<span style="color: black;">更加多</span>PHP大厂PDF面试文档,PHP进阶架构视频资料,PHP精彩好文免费获取<span style="color: black;">能够</span>关注公众号:PHP开源社区,<span style="color: black;">或</span><span style="color: black;">拜访</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">2021金三银四大厂面试真题集锦,必</a>看!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">四年精华PHP技术<span style="color: black;">文案</span>整理合集——PHP框架篇</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">四年精华PHP技术文合集——微服务架构篇</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">四年精华PHP技术文合集——分布式架构篇</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">四年精华PHP技术文合集——高并发场景篇</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">四年精华PHP技术<span style="color: black;">文案</span>整理合集——数据库篇</a></p>
说得好啊!我在外链论坛打滚这么多年,所谓阅人无数,就算没有见过猪走路,也总明白猪肉是啥味道的。 感谢楼主的分享!我学到了很多。
页:
[1]