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;"><span style="color: black;">function</span> <span style="color: black;">sign</span><span style="color: black;">(</span><span style="color: black;">$data</span><span style="color: black;">)</span> <span style="color: black;">{</span>
<span style="color: black;">//读取私钥文件
</span> <span style="color: black;">$priKey</span> <span style="color: black;">=</span> <span style="color: black;">file_get_contents</span><span style="color: black;">(</span><span style="color: black;">key/rsa_private_key.pem</span><span style="color: black;">);</span>
<span style="color: black;">//转换为openssl密钥,<span style="color: black;">必要</span>是<span style="color: black;">无</span>经过pkcs8转换的私钥</span> <span style="color: black;">$res</span> <span style="color: black;">=</span> <span style="color: black;">openssl_get_privatekey</span><span style="color: black;">(</span><span style="color: black;">$priKey</span><span style="color: black;">);</span>
<span style="color: black;">//调用openssl内置签名<span style="color: black;">办法</span>,生成签名$sign
</span> <span style="color: black;">openssl_sign</span><span style="color: black;">(</span><span style="color: black;">$data</span><span style="color: black;">,</span> <span style="color: black;">$sign</span><span style="color: black;">,</span> <span style="color: black;">$res</span><span style="color: black;">);</span>
<span style="color: black;">//释放资源
</span> <span style="color: black;">openssl_free_key</span><span style="color: black;">(</span><span style="color: black;">$res</span><span style="color: black;">);</span>
<span style="color: black;">return</span> <span style="color: black;">$sign</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
</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;"><span style="color: black;">function</span> <span style="color: black;">verify</span><span style="color: black;">(</span><span style="color: black;">$data</span><span style="color: black;">,</span> <span style="color: black;">$sign</span><span style="color: black;">)</span> <span style="color: black;">{</span>
<span style="color: black;">//读取支付宝公钥文件
</span> <span style="color: black;">$pubKey</span> <span style="color: black;">=</span> <span style="color: black;">file_get_contents</span><span style="color: black;">(</span><span style="color: black;">key/alipay_public_key.pem</span><span style="color: black;">);</span>
<span style="color: black;">//转换为openssl格式密钥
</span> <span style="color: black;">$res</span> <span style="color: black;">=</span> <span style="color: black;">openssl_get_publickey</span><span style="color: black;">(</span><span style="color: black;">$pubKey</span><span style="color: black;">);</span>
<span style="color: black;">//调用openssl内置<span style="color: black;">办法</span>验签,返回bool值
</span> <span style="color: black;">$result</span> <span style="color: black;">=</span> <span style="color: black;">(</span><span style="color: black;">bool</span><span style="color: black;">)</span><span style="color: black;">openssl_verify</span><span style="color: black;">(</span><span style="color: black;">$data</span><span style="color: black;">,</span> <span style="color: black;">$sign</span><span style="color: black;">,</span> <span style="color: black;">$res</span><span style="color: black;">);</span>
<span style="color: black;">//释放资源
</span> <span style="color: black;">openssl_free_key</span><span style="color: black;">(</span><span style="color: black;">$res</span><span style="color: black;">);</span>
<span style="color: black;">return</span> <span style="color: black;">$result</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
</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;"><span style="color: black;">function</span> <span style="color: black;">decrypt</span><span style="color: black;">(</span><span style="color: black;">$content</span><span style="color: black;">)</span> <span style="color: black;">{</span>
<span style="color: black;">//读取商户私钥
</span> <span style="color: black;">$priKey</span> <span style="color: black;">=</span> <span style="color: black;">file_get_contents</span><span style="color: black;">(</span><span style="color: black;">key/rsa_private_key.pem</span><span style="color: black;">);</span>
<span style="color: black;">//转换为openssl密钥,<span style="color: black;">必要</span>是<span style="color: black;">无</span>经过pkcs8转换的私钥
</span> <span style="color: black;">$res</span> <span style="color: black;">=</span> <span style="color: black;">openssl_get_privatekey</span><span style="color: black;">(</span><span style="color: black;">$priKey</span><span style="color: black;">);</span>
<span style="color: black;">//声明明文字符串变量</span> <span style="color: black;">$result</span> <span style="color: black;">=</span> <span style="color: black;">;</span>
<span style="color: black;">//循环<span style="color: black;">根据</span>128位解密
</span> <span style="color: black;">for</span><span style="color: black;">(</span><span style="color: black;">$i</span> <span style="color: black;">=</span> <span style="color: black;">0</span><span style="color: black;">;</span> <span style="color: black;">$i</span> <span style="color: black;"><</span> <span style="color: black;">strlen</span><span style="color: black;">(</span><span style="color: black;">$content</span><span style="color: black;">)</span><span style="color: black;">/</span><span style="color: black;">128</span><span style="color: black;">;</span> <span style="color: black;">$i</span><span style="color: black;">++</span> <span style="color: black;">)</span> <span style="color: black;">{</span>
<span style="color: black;">$data</span> <span style="color: black;">=</span> <span style="color: black;">substr</span><span style="color: black;">(</span><span style="color: black;">$content</span><span style="color: black;">,</span> <span style="color: black;">$i</span> <span style="color: black;">*</span> <span style="color: black;">128</span><span style="color: black;">,</span> <span style="color: black;">128</span><span style="color: black;">);</span>
<span style="color: black;">//拆<span style="color: black;">掰开</span>长度为128的字符串片段<span style="color: black;">经过</span>私钥进行解密,返回$decrypt解析后的明文
</span> <span style="color: black;">openssl_private_decrypt</span><span style="color: black;">(</span><span style="color: black;">$data</span><span style="color: black;">,</span> <span style="color: black;">$decrypt</span><span style="color: black;">,</span> <span style="color: black;">$res</span><span style="color: black;">);</span>
<span style="color: black;">//明文片段拼接
</span> <span style="color: black;">$result</span> <span style="color: black;">.=</span> <span style="color: black;">$decrypt</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;">//释放资源
</span> <span style="color: black;">openssl_free_key</span><span style="color: black;">(</span><span style="color: black;">$res</span><span style="color: black;">);</span>
<span style="color: black;">//返回明文
</span> <span style="color: black;">return</span> <span style="color: black;">$result</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
</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><span style="color: black;">更加多</span><span style="color: black;">文案</span><span style="color: black;">能够</span>关注公众号:PHP从入门到精通,<span style="color: black;">非常多</span>PHPer在进阶的时候总会遇到<span style="color: black;">有些</span>问题和瓶颈,业务代码写多了<span style="color: black;">无</span>方向感,不<span style="color: black;">晓得</span>该从那里入手去<span style="color: black;">提高</span>,对此我整理了<span style="color: black;">有些</span>PHP高级、架构视频资料和大厂PHP面试PDF免费获取,<span style="color: black;">必须</span>戳<span style="color: black;">这儿</span><a style="color: black;">PHP进阶架构师>>>实战视频、大厂面试文档免费获取</a></p>
论坛的成功是建立在我们诚恳、务实、高效、创新和团结合作基础上,我们要把这种精神传递下去。 我完全赞同你的观点,思考很有深度。
页:
[1]