C# 使用正则表达式(dotnetcore)_"regex rx = new regex(@\"
赞
踩
1.使用支付宝SDK,本次使用的SDK是老版
支付宝文档:https://opendocs.alipay.com/open/54/103419
首先下载或使用新上线的Web在线加密,生成应用私钥与应用公钥
以Web在线加密为示例:https://miniu.alipay.com/keytool/create
支付宝SDK代码示例:
目录结构:
代码片段:
- <?php
- //引入
- require_once '../AopClient.php';
-
- class SignTest
- {
- protected $aop;
- protected $private_key;
- protected $alipayrsa_public_key;
-
- public function __construct()
- {
- $aop = new AopClient();
- $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
-
- $aop->appId = '2021';
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA2';
- $aop->postCharset = 'utf-8';
- $aop->format = 'json';
-
- //应用私钥
- $private_key = 'MIIEpAIBAAKCAQEAwd+e6eZAEW9u0zaOKog5orZMgXR9duL3L+mwUCwb3AntIz6Ofst/AX5TS3g74crjXvfZgD5JMrBoRhPFFxCBb66MfxViinr41J0n0LxHlm/UYAMVcN0795lV0MRmJ9PiplCa/3mxG7prAPukqxAFM5BYvaTBucVSgRKrUVODouxcQuuFvr9uNMNS/T03/mSgWJTRtUHg1Wz9bncnFJUmr56ij10yYIAQD8FjGuga87jKi79/DnSgeF90qUoXeCY6ezlsO8NJ4pwVPqppWBxRSDok4qCPYnPLRmswFB46GViXmL6b4UnI/gxUSj6pFPhq6b3J648sz5RgfTzodOipOQIDAQABAoIBAEFbLkoxaZW3GAgx0vk5lRxqEh5t88G5P5UyVVKhm/6whfWkqArMdRM+z7uYW6+BYubn7LWTyb9BwtUk4wMkeE367LgGsIJ4dBvQotR2NDfdmR2IzJiQrB+K3GgVXU692iEw+R94maD8VggtzNQyAV7hoVjFCt2iTG7gqUowOicYqV+W0fbnhKgDVcRGr6ecybvtTe/VkLpehOwFvUacNnAK3xOCaXAiPu7jM71Uouu0cvs5ypFPp7evpeHnEhgQ5YVT6cnMdBqL2ZC/BxO0KUipraBe1rLvrJw1R6fbwgtjXmpcCX0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3g74crjXvfZgD5JMrBoRhPFFxCBb66MfxViinr41J0n0LxHlm/UYAMVcN0795lV0MRmJ9PiplCa/3mxG7prAPukqxAFM5BYvaTBucVSgRKrUVODouxcQuuFvr9uNMNS/T03/mSgWJTRtUHg1Wz9bncnFJUmr56ij10yYIAQD8FjGuga87jKi79/DnSgeF90qUoXeCY6ezlsO8NJ4pwVPqppWBxRSDok4qCPYnPLRmswFB46GViXmL6b4UnI/gxUSj6pFPhq6b3J648sz5RgfTzodOipOQIDAQAB';
-
- $aop->alipayrsaPublicKey = $alipayrsa_public_key;
- $this->aop = $aop;
- $this->private_key = $private_key;
- $this->alipayrsa_public_key = $alipayrsa_public_key;
- }
-
- //生成签名与验签
- public function createSign($type, $sign, $time)
- {
- $content = array();
- $content['a'] = "1";
- $con = json_encode($content);
- $param['app_id'] = '2021';
- $param['method'] = 'www.baidu.com';//接口名称,固定值
-
- $param['charset'] = 'UTF-8';//请求使用的编码格式
-
- $param['sign_type'] = 'RSA2';//商户生成签名字符串所使用的签名算法类型
-
- $param['timestamp'] = $time;//发送请求的时间
-
- $param['version'] = '1.0';//调用的接口版本,固定为:1.0
-
- $param['biz_content'] = $con;//业务请求参数的集合,长度不限,json格式,即前面一步得到的
-
- //类型为1 生成签名
- if ($type == 1) {
- $paramStr = $this->aop->getSignContent($param);//组装请求签名参数
- $res = $this->aop->alonersaSign($paramStr, $this->private_key, 'RSA2', false);//生成签名
- return $res;
- } else {
- //类型为2 验证签名,须注意验证签名,就是生成签名的原始数据传回,并把生成的签名传入
- $param['sign'] = $sign;
- $res = $this->aop->rsaCheckV2($param, $this->alipayrsa_public_key, 'RSA2');
- return $res;
- }
- }
-
- //数据加密
- public function jiami()
- {
- // 加密内容必须为对象信息
- $str = '{"a":112}';
- $res = $this->aop->rsaEncrypt($str, $this->alipayrsa_public_key, 'UTF-8');
- return $res;
- }
-
- //数据解密
- public function jiemi($sign)
- {
- // 解密须先 base64_decode
- $res = $this->aop->rsaDecrypt(base64_decode($sign, true), $this->private_key, 'UTF-8');
- return $res;
- }
- }
-
- $type = $_POST['type'];
- $aop = new SignTest();
- //1为生成签名,2为验证签名
- if ($type == 1 || $type == 2) {
- $sign = $_POST['sign'];
- $time = $_POST['time'];
- $res = $aop->createSign($type, $sign, $time);
- echo $res;
- }
- //数据加密
- if ($type == 3) {
- $res = $aop->jiami();
- echo $res;
- }
- //数据解密
- if ($type == 4) {
- // 这是数据加密的值
- $sign = $_POST['sign'];
- $res = $aop->jiemi($sign);
- echo $res;
- }
-
-
PHP原生写法示例:
- class SignTest2
- {
- // 应用公钥
- private $public_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhGYnCecQix/2Pllw5UXMKF3RgryqxXWrgoace8zUumagwX/WMeI0ZjaVFtrkUdiMpANILpB/p1u0AJAifxQuJSijR2a0oQ4zIoFbjm/HDipRcXbkeuMX4gRk863zBevfu0nNAHSReNLu9BaDwAN1OzJPWtxTFtrQ1lzKRIaVCCqDs2WAIlv979G7GQfOETWrgOr1Vy6s69VhGeKgz9+D/MOhWkaTiE+06JesIgH+ohfsH4G+Evz1kf1UYZ+ax3psQZEno3x24fAJt4CGuqj157zrb7OpRAWf/VWT8OAkp6G/y3c+YW22JcAEKlhBjijN8Y/LMoFOfeo7db0kQ51vgwIDAQAB';
-
- //应用私钥
- private $private_key = 'MIIEpAIBAAKCAQEAhGYnCecQix/2Pllw5UXMKF3RgryqxXWrgoace8zUumagwX/WMeI0ZjaVFtrkUdiMpANILpB/p1u0AJAifxQuJSijR2a0oQ4zIoFbjm/HDipRcXbkeuMX4gRk863zBevfu0nNAHSReNLu9BaDwAN1OzJPWtxTFtrQ1lzKRIaVCCqDs2WAIlv979G7GQfOETWrgOr1Vy6s69VhGeKgz9+D/MOhWkaTiE+06JesIgH+ohfsH4G+Evz1kf1UYZ+ax3psQZEno3x24fAJt4CGuqj157zrb7OpRAWf/VWT8OAkp6G/y3c+YW22JcAEKlhBjijN8Y/LMoFOfeo7db0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WF3POYdMYGWRwNgt04277/uI8gcJt0NCBJ1HvAGo6D8uT+l//8l9tMs8eOwGSIg5xdwA4v2UOgb1N10Qm1x3/T2D+V1+RO4mrDGUpSmBDVtQYnDIdjfRitERUI1+1HIRPJ7qBMsQbSc9ySZ5WE4Aroq/zDmBha2A==';
-
- // 数据加密
- public function jiami()
- {
- $publicKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($this->public_key, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
- //需要加密的数据
- $data = [
- 'order_no' => '20212193123',
- 'money' => 99,
- 'user_id' => 12,
- 'pay_time' => '2021-6-18 17:02:00'
- ];
-
- $publicKey = openssl_pkey_get_public($publicKey); //解析公钥
- $res = openssl_public_encrypt(json_encode($data), $encrypted, $publicKey) ? base64_encode($encrypted) : null;
- return $res;
- }
-
- // 解密
- public function jiemi($encrypted)
- {
- $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . $this->private_key . "\n-----END RSA PRIVATE KEY-----";
- $key = openssl_pkey_get_private($privateKey); //解析私钥
- $encrypted = base64_decode($encrypted);
- //解密
- $res = openssl_private_decrypt($encrypted, $decrypted, $key) ? $decrypted : null;
- return $res;
- }
-
- //生成签名
- public function qianming()
- {
- //要签名的数据
- $data = [
- 'order_no' => '20212193123',
- 'money' => 99,
- 'user_id' => 12,
- 'pay_time' => '2021-6-18 17:02:00'
- ];
- $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . $this->private_key . "\n-----END RSA PRIVATE KEY-----";
- $privateKey = openssl_pkey_get_private($privateKey);
- $res = openssl_sign(json_encode($data), $sign, $privateKey, OPENSSL_ALGO_SHA256) ? base64_encode($sign) : null;
- return $res;
- }
-
- //验签
- public function yanqian($sign)
- {
- //原始数据
- $data = [
- 'order_no' => '20212193123',
- 'money' => 99,
- 'user_id' => 12,
- 'pay_time' => '2021-6-18 17:02:00'
- ];
- $publicKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($this->public_key, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
- $publicKey = openssl_pkey_get_public($publicKey);
- $res = openssl_verify(json_encode($data), base64_decode($sign), $publicKey, OPENSSL_ALGO_SHA256);
- return $res ? '正确' : '错误';
- }
- }
-
- $test = new SignTest2();
- $type1 = $_POST['type1'];
- //数据加密
- if ($type1 == 1) {
- $res = $test->jiami();
- echo $res;
- }
- //加密数据解密
- if ($type1 == 2) {
- $sign = $_POST['sign'];
- $res = $test->jiemi($sign);
- echo $res;
- }
- //数据加密
- if ($type1 == 3) {
- $res = $test->qianming();
- echo $res;
- }
- //加密数据解密
- if ($type1 == 4) {
- $sign = $_POST['sign'];
- $res = $test->yanqian($sign);
- echo $res;
- }
写在最后:
这只是记录加密与解密,签名与验签的简单使用,供学习参考,谢谢!
可以相互交流学习一下哦。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。