PHP支付宝接口RSA验证
这两天一直困扰的PHP RSA签名验证问题终于解决了,由于之前RSA接触的不多,再加上官方至今还未有PHP的SDK可供参考,因此走了一些弯路,写在这里和大家分享。
虽然支付宝官方还未提供相关SDK,PHP确实可以实现RSA方式的签名,这点其实很重要,由于不熟悉,在遇到困难的时候,经常会不由自主地想到是否PHP不支持RSA签名,干脆用MD5得了,这样就没有了前进的动力。其实说穿了MD5和RSA签名,不同的只是签名方式的区别,其他的都一样,因此我这里主要说一下如何用RSA进行签名和验签。
首先你需要准备下面的东西:
php的openssl扩展里已经封装好了验签的方法openssl_verify。
如果在Windows下的php.ini需要开启Openssl模块: extension=php_openssl.dll
商户私钥:
即RSA私钥,按照手册,按以下方式生成:
openssl genrsa -out rsa_private_key.pem 1024
商户公钥:
即RSA私钥,按照手册,按以下方式生成:
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
生成之后,按照手册的说明,需要在签约平台上传公钥,需要注意的是,上传的时候需要把所有的注释和换行都去掉。
另外手册中还有如下命令:
openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt
该命令将RSA私钥转换成PKCS8格式,对于PHP来说,不需要。
支付宝公钥:
根据手册,在签约平台获得。
如果你直接复制下来的话,会得到一个字符串,需要进行下面的转换;
1)把空格变成换行
2)添加注释
比如你复制下来的公钥是:MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDRBMjkaBznjXk06ddsL751KyYt
ztPFg0D3tu7jLqCacgqL+lbshIaItDGEXAMZmKa3DV6Wxy+l48YMo0RyS+dWze4M
UmuxHU/v6tiT0ZTXJN3EwrjCtCyyttdv/ROB3CkheXnTKB76reTkQqg57OWW+m9j
TCoccYMDXEIWYTs3CwIDAQAB,那转换之后为:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDRBMjkaBznjXk06ddsL751KyYt
ztPFg0D3tu7jLqCacgqL+lbshIaItDGEXAMZmKa3DV6Wxy+l48YMo0RyS+dWze4M
UmuxHU/v6tiT0ZTXJN3EwrjCtCyyttdv/ROB3CkheXnTKB76reTkQqg57OWW+m9j
TCoccYMDXEIWYTs3CwIDAQAB
-----END PUBLIC KEY-----
把公钥保存在文件里。
注意这个是2048位的公钥应该是9行或者10行,不能为1行,不然PHP的openssl_pkey_get_public无法读取,pub_key_id的结果为false,如果没有-----BEGIN PUBLIC KEY----- 和 -----END PUBLIC KEY----- 可以自己加上,最后保存到一个rsa_public_key.pem文件中。
好了,现在已经有了所有的东西,先看签名函数:
复制代码
1
2 /**
3 * 签名字符串
4 * @param $prestr 需要签名的字符串
5 * return 签名结果
6 */
7 function rsaSign($prestr) {
8 $public_key= file_get_contents('rsa_private_key.pem');
9 $pkeyid = openssl_get_privatekey($public_key);
10 openssl_sign($prestr, $sign, $pkeyid);
11 openssl_free_key($pkeyid);
12 $sign = base64_encode($sign);
13 return $sign;
14 }
15 ?>
复制代码
注意点:
1.$prestr的内容和MD5一样(参见手册,但不包含最后的MD5密码)
2.签名用商户私钥
3.最后的签名,需要用base64编码
4.这个函数返回的值,就是这次请求的RSA签名。
验签函数:
复制代码
1
2 /**
3 * 验证签名
4 * @param $prestr 需要签名的字符串
5 * @param $sign 签名结果
6 * return 签名结果
7 */
8 function rsaVerify($prestr, $sign) {
9 $sign = base64_decode($sign);
10 $public_key= file_get_contents('rsa_public_key.pem');
11 $pkeyid = openssl_get_publickey($public_key);
12 if ($pkeyid) {
13 $verify = openssl_verify($prestr, $sign, $pkeyid);
14 openssl_free_key($pkeyid);
15 }
16 if($verify == 1){
17 return true;
18 }else{
19 return false;
20 }
21 }
22 ?>
复制代码
注意点:
1.$prestr的内容和MD5一样(参见手册)
2.$sign是支付宝接口返回的sign参数用base64_decode解码之后的二进制
3.验签用支付宝公钥
4.这个函数返回一个布尔值,直接告诉你,验签是否通过
支付宝官方提供的PHP版SDK demo中只对MD5加密方式进行了处理,但android 端和ios端 请求支付宝加密方式只能用RSA加密算法,这时服务端PHP就无法验证签名了,所以需要对demo进行一些修改。
1、修改alipay_notify.class.php文件
verifyNotify 函数第46行
$isSign = $this->getSignVeryfy($_POST, $_POST["sign"]);
改成
$isSign = $this->getSignVeryfy($_POST, $_POST["sign"], $_POST["sign_type"]);
verifyReturn 函数第83行
$isSign = $this->getSignVeryfy($_GET, $_GET["sign"]);
改成
$isSign = $this->getSignVeryfy($_GET, $_GET["sign"], $_GET["sign_type"]);
getSignVeryfy 函数 116行
function getSignVeryfy($para_temp, $sign) {
改成
function getSignVeryfy($para_temp, $sign, $sign_type) {
getSignVeryfy 函数 127行
switch (strtoupper(trim($this->alipay_config['sign_type']))) {
case "MD5" :
$isSgin = md5Verify($prestr, $sign, $this->alipay_config['key']);
break;
default :
$isSgin = false;
}
改成
switch (strtoupper(trim($sign_type))) {
case "MD5" :
$isSgin = md5Verify($prestr, $sign, $this->alipay_config['key']);
break;
case "RSA" :
$isSgin = rsaVerify($prestr, $sign);
break;
default :
$isSgin = false;
}
2、新建一个alipay_rsa.function.php文件
复制代码
1
2 /* *
3 * RSA
4 * 详细:RSA加密
5 * 版本:3.3
6 * 日期:2014-02-20
7 * 说明:
8 * 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
9 * 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
10 */
11 /**
12 * 签名字符串
13 * @param $prestr 需要签名的字符串
14 * return 签名结果
15 */
16 function rsaSign($prestr) {
17 $public_key= file_get_contents('rsa_private_key.pem');
18 $pkeyid = openssl_get_privatekey($public_key);
19 openssl_sign($prestr, $sign, $pkeyid);
20 openssl_free_key($pkeyid);
21 $sign = base64_encode($sign);
22 return $sign;
23 }
24 /**
25 * 验证签名
26 * @param $prestr 需要签名的字符串
27 * @param $sign 签名结果
28 * return 签名结果
29 */
30 function rsaVerify($prestr, $sign) {
31 $sign = base64_decode($sign);
32 $public_key= file_get_contents('rsa_public_key.pem');
33 $pkeyid = openssl_get_publickey($public_key);
34 if ($pkeyid) {
35 $verify = openssl_verify($prestr, $sign, $pkeyid);
36 openssl_free_key($pkeyid);
37 }
38 if($verify == 1){
39 return true;
40 }else{
41 return false;
42 }
43 }
44 ?>
复制代码
最后要说的是官方提供的手册上说的基本上都是正确的,只是有些地方没有说的很详细,开发的时候一定要多参考,大致就是这样,祝大家好运。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.
