当前位置:   article > 正文

PHP 支付宝SDK加密与解密、签名与验签(含php原生写法)_php 支付宝 spi加解密

php 支付宝 spi加解密

1.使用支付宝SDK,本次使用的SDK是老版

支付宝文档:https://opendocs.alipay.com/open/54/103419

首先下载或使用新上线的Web在线加密,生成应用私钥与应用公钥

以Web在线加密为示例:https://miniu.alipay.com/keytool/create

支付宝SDK代码示例:

目录结构:


代码片段:

  1. <?php
  2. //引入
  3. require_once '../AopClient.php';
  4. class SignTest
  5. {
  6. protected $aop;
  7. protected $private_key;
  8. protected $alipayrsa_public_key;
  9. public function __construct()
  10. {
  11. $aop = new AopClient();
  12. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  13. $aop->appId = '2021';
  14. $aop->apiVersion = '1.0';
  15. $aop->signType = 'RSA2';
  16. $aop->postCharset = 'utf-8';
  17. $aop->format = 'json';
  18. //应用私钥
  19. $private_key = 'MIIEpAIBAAKCAQEAwd+e6eZAEW9u0zaOKog5orZMgXR9duL3L+mwUCwb3AntIz6Ofst/AX5TS3g74crjXvfZgD5JMrBoRhPFFxCBb66MfxViinr41J0n0LxHlm/UYAMVcN0795lV0MRmJ9PiplCa/3mxG7prAPukqxAFM5BYvaTBucVSgRKrUVODouxcQuuFvr9uNMNS/T03/mSgWJTRtUHg1Wz9bncnFJUmr56ij10yYIAQD8FjGuga87jKi79/DnSgeF90qUoXeCY6ezlsO8NJ4pwVPqppWBxRSDok4qCPYnPLRmswFB46GViXmL6b4UnI/gxUSj6pFPhq6b3J648sz5RgfTzodOipOQIDAQABAoIBAEFbLkoxaZW3GAgx0vk5lRxqEh5t88G5P5UyVVKhm/6whfWkqArMdRM+z7uYW6+BYubn7LWTyb9BwtUk4wMkeE367LgGsIJ4dBvQotR2NDfdmR2IzJiQrB+K3GgVXU692iEw+R94maD8VggtzNQyAV7hoVjFCt2iTG7gqUowOicYqV+W0fbnhKgDVcRGr6ecybvtTe/VkLpehOwFvUacNnAK3xOCaXAiPu7jM71Uouu0cvs5ypFPp7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BCgKCAQEAwd+e6eZAEW9u0zaOKog5orZMgXR9duL3L+mwUCwb3AntIz6Ofst/AX5TS3g74crjXvfZgD5JMrBoRhPFFxCBb66MfxViinr41J0n0LxHlm/UYAMVcN0795lV0MRmJ9PiplCa/3mxG7prAPukqxAFM5BYvaTBucVSgRKrUVODouxcQuuFvr9uNMNS/T03/mSgWJTRtUHg1Wz9bncnFJUmr56ij10yYIAQD8FjGuga87jKi79/DnSgeF90qUoXeCY6ezlsO8NJ4pwVPqppWBxRSDok4qCPYnPLRmswFB46GViXmL6b4UnI/gxUSj6pFPhq6b3J648sz5RgfTzodOipOQIDAQAB';
  23. $aop->alipayrsaPublicKey = $alipayrsa_public_key;
  24. $this->aop = $aop;
  25. $this->private_key = $private_key;
  26. $this->alipayrsa_public_key = $alipayrsa_public_key;
  27. }
  28. //生成签名与验签
  29. public function createSign($type, $sign, $time)
  30. {
  31. $content = array();
  32. $content['a'] = "1";
  33. $con = json_encode($content);
  34. $param['app_id'] = '2021';
  35. $param['method'] = 'www.baidu.com';//接口名称,固定值
  36. $param['charset'] = 'UTF-8';//请求使用的编码格式
  37. $param['sign_type'] = 'RSA2';//商户生成签名字符串所使用的签名算法类型
  38. $param['timestamp'] = $time;//发送请求的时间
  39. $param['version'] = '1.0';//调用的接口版本,固定为:1.0
  40. $param['biz_content'] = $con;//业务请求参数的集合,长度不限,json格式,即前面一步得到的
  41. //类型为1 生成签名
  42. if ($type == 1) {
  43. $paramStr = $this->aop->getSignContent($param);//组装请求签名参数
  44. $res = $this->aop->alonersaSign($paramStr, $this->private_key, 'RSA2', false);//生成签名
  45. return $res;
  46. } else {
  47. //类型为2 验证签名,须注意验证签名,就是生成签名的原始数据传回,并把生成的签名传入
  48. $param['sign'] = $sign;
  49. $res = $this->aop->rsaCheckV2($param, $this->alipayrsa_public_key, 'RSA2');
  50. return $res;
  51. }
  52. }
  53. //数据加密
  54. public function jiami()
  55. {
  56. // 加密内容必须为对象信息
  57. $str = '{"a":112}';
  58. $res = $this->aop->rsaEncrypt($str, $this->alipayrsa_public_key, 'UTF-8');
  59. return $res;
  60. }
  61. //数据解密
  62. public function jiemi($sign)
  63. {
  64. // 解密须先 base64_decode
  65. $res = $this->aop->rsaDecrypt(base64_decode($sign, true), $this->private_key, 'UTF-8');
  66. return $res;
  67. }
  68. }
  69. $type = $_POST['type'];
  70. $aop = new SignTest();
  71. //1为生成签名,2为验证签名
  72. if ($type == 1 || $type == 2) {
  73. $sign = $_POST['sign'];
  74. $time = $_POST['time'];
  75. $res = $aop->createSign($type, $sign, $time);
  76. echo $res;
  77. }
  78. //数据加密
  79. if ($type == 3) {
  80. $res = $aop->jiami();
  81. echo $res;
  82. }
  83. //数据解密
  84. if ($type == 4) {
  85. // 这是数据加密的值
  86. $sign = $_POST['sign'];
  87. $res = $aop->jiemi($sign);
  88. echo $res;
  89. }

PHP原生写法示例:

  1. class SignTest2
  2. {
  3. // 应用公钥
  4. private $public_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhGYnCecQix/2Pllw5UXMKF3RgryqxXWrgoace8zUumagwX/WMeI0ZjaVFtrkUdiMpANILpB/p1u0AJAifxQuJSijR2a0oQ4zIoFbjm/HDipRcXbkeuMX4gRk863zBevfu0nNAHSReNLu9BaDwAN1OzJPWtxTFtrQ1lzKRIaVCCqDs2WAIlv979G7GQfOETWrgOr1Vy6s69VhGeKgz9+D/MOhWkaTiE+06JesIgH+ohfsH4G+Evz1kf1UYZ+ax3psQZEno3x24fAJt4CGuqj157zrb7OpRAWf/VWT8OAkp6G/y3c+YW22JcAEKlhBjijN8Y/LMoFOfeo7db0kQ51vgwIDAQAB';
  5. //应用私钥
  6. private $private_key = 'MIIEpAIBAAKCAQEAhGYnCecQix/2Pllw5UXMKF3RgryqxXWrgoace8zUumagwX/WMeI0ZjaVFtrkUdiMpANILpB/p1u0AJAifxQuJSijR2a0oQ4zIoFbjm/HDipRcXbkeuMX4gRk863zBevfu0nNAHSReNLu9BaDw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a+ggFToPiA9u/b0aA+OY0bfgfhoumNFur+IYdbAYtan3WB9k2wV7kG3DlhFbF12HvMqjnBO8ZpVCcKX7/3hu9/r8szCALIM46LubhlRHXXCTcLa5WP2Xu9Xb1L/BUXG+Bcaxv5HclmKXgUUjdHT9ZQRK9pKXw53NAsMcXp2YqwKBgQCbpJs1vYCwbNWF3POYdMYGWRwNgt04277/uI8gcJt0NCBJ1HvAGo6D8uT+l//8l9tMs8eOwGSIg5xdwA4v2UOgb1N10Qm1x3/T2D+V1+RO4mrDGUpSmBDVtQYnDIdjfRitERUI1+1HIRPJ7qBMsQbSc9ySZ5WE4Aroq/zDmBha2A==';
  7. // 数据加密
  8. public function jiami()
  9. {
  10. $publicKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($this->public_key, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
  11. //需要加密的数据
  12. $data = [
  13. 'order_no' => '20212193123',
  14. 'money' => 99,
  15. 'user_id' => 12,
  16. 'pay_time' => '2021-6-18 17:02:00'
  17. ];
  18. $publicKey = openssl_pkey_get_public($publicKey); //解析公钥
  19. $res = openssl_public_encrypt(json_encode($data), $encrypted, $publicKey) ? base64_encode($encrypted) : null;
  20. return $res;
  21. }
  22. // 解密
  23. public function jiemi($encrypted)
  24. {
  25. $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . $this->private_key . "\n-----END RSA PRIVATE KEY-----";
  26. $key = openssl_pkey_get_private($privateKey); //解析私钥
  27. $encrypted = base64_decode($encrypted);
  28. //解密
  29. $res = openssl_private_decrypt($encrypted, $decrypted, $key) ? $decrypted : null;
  30. return $res;
  31. }
  32. //生成签名
  33. public function qianming()
  34. {
  35. //要签名的数据
  36. $data = [
  37. 'order_no' => '20212193123',
  38. 'money' => 99,
  39. 'user_id' => 12,
  40. 'pay_time' => '2021-6-18 17:02:00'
  41. ];
  42. $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . $this->private_key . "\n-----END RSA PRIVATE KEY-----";
  43. $privateKey = openssl_pkey_get_private($privateKey);
  44. $res = openssl_sign(json_encode($data), $sign, $privateKey, OPENSSL_ALGO_SHA256) ? base64_encode($sign) : null;
  45. return $res;
  46. }
  47. //验签
  48. public function yanqian($sign)
  49. {
  50. //原始数据
  51. $data = [
  52. 'order_no' => '20212193123',
  53. 'money' => 99,
  54. 'user_id' => 12,
  55. 'pay_time' => '2021-6-18 17:02:00'
  56. ];
  57. $publicKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($this->public_key, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
  58. $publicKey = openssl_pkey_get_public($publicKey);
  59. $res = openssl_verify(json_encode($data), base64_decode($sign), $publicKey, OPENSSL_ALGO_SHA256);
  60. return $res ? '正确' : '错误';
  61. }
  62. }
  63. $test = new SignTest2();
  64. $type1 = $_POST['type1'];
  65. //数据加密
  66. if ($type1 == 1) {
  67. $res = $test->jiami();
  68. echo $res;
  69. }
  70. //加密数据解密
  71. if ($type1 == 2) {
  72. $sign = $_POST['sign'];
  73. $res = $test->jiemi($sign);
  74. echo $res;
  75. }
  76. //数据加密
  77. if ($type1 == 3) {
  78. $res = $test->qianming();
  79. echo $res;
  80. }
  81. //加密数据解密
  82. if ($type1 == 4) {
  83. $sign = $_POST['sign'];
  84. $res = $test->yanqian($sign);
  85. echo $res;
  86. }

写在最后:

这只是记录加密与解密,签名与验签的简单使用,供学习参考,谢谢!

可以相互交流学习一下哦。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/122892?site
推荐阅读
相关标签
  

闽ICP备14008679号