源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

PHP实现超简单的SSL加密解密、验证及签名的方法示例

  • 时间:2021-06-23 13:06 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:PHP实现超简单的SSL加密解密、验证及签名的方法示例
本文实例讲述了PHP实现超简单的SSL加密解密、验证及签名的方法。分享给大家供大家参考,具体如下: 1. sign签名代码:
function sign($data) {
  //读取私钥文件
  $priKey = file_get_contents('key/rsa_private_key.pem');
  //转换为openssl密钥,必须是没有经过pkcs8转换的私钥
  $res = openssl_get_privatekey($priKey);
  //调用openssl内置签名方法,生成签名$sign
  openssl_sign($data, $sign, $res);
  //释放资源
  openssl_free_key($res);
  return $sign;
}

2. verify 验证代码:
function verify($data, $sign) {
  //读取支付宝公钥文件
  $pubKey = file_get_contents('key/alipay_public_key.pem');
  //转换为openssl格式密钥
  $res = openssl_get_publickey($pubKey);
  //调用openssl内置方法验签,返回bool值
  $result = (bool)openssl_verify($data, $sign, $res);
  //释放资源
  openssl_free_key($res);
  return $result;
}

3. 解密代码
function decrypt($content) {
  //读取商户私钥
  $priKey = file_get_contents('key/rsa_private_key.pem');
  //转换为openssl密钥,必须是没有经过pkcs8转换的私钥
  $res = openssl_get_privatekey($priKey);
  //声明明文字符串变量
  $result = '';
  //循环按照128位解密
  for($i = 0; $i < strlen($content)/128; $i++ ) {
    $data = substr($content, $i * 128, 128);
  //拆分开长度为128的字符串片段通过私钥进行解密,返回$decrypt解析后的明文
    openssl_private_decrypt($data, $decrypt, $res);
  //明文片段拼接
    $result .= $decrypt;
  }
  //释放资源
  openssl_free_key($res);
  //返回明文
  return $result;
}

[b]PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:[/b] [b]文字在线加密解密工具(包含AES、DES、RC4等): [/b][url=http://tools.jb51.net/password/txt_encode]http://tools.jb51.net/password/txt_encode[/url] [b]MD5在线加密工具: [/b][url=http://tools.jb51.net/password/CreateMD5Password]http://tools.jb51.net/password/CreateMD5Password[/url] [b]在线散列/哈希算法加密工具: [/b][url=http://tools.jb51.net/password/hash_encrypt]http://tools.jb51.net/password/hash_encrypt[/url] [b]在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具: [/b][url=http://tools.jb51.net/password/hash_md5_sha]http://tools.jb51.net/password/hash_md5_sha[/url] [b]在线sha1/sha224/sha256/sha384/sha512加密工具: [/b][url=http://tools.jb51.net/password/sha_encode]http://tools.jb51.net/password/sha_encode[/url] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/144.htm]php加密方法总结[/url]》、《[url=http://www.1sucai.cn/Special/459.htm]PHP编码与转码操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/614.htm]PHP数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/111.htm]php程序设计算法总结[/url]》及《[url=http://www.1sucai.cn/Special/180.htm]php正则表达式用法总结[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部