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

AI Hentai Generator
Generate AI Hentai for free.

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.

For investors new to the cryptocurrency world, it is crucial to understand how to buy virtual coins. This article is designed to provide a comprehensive guide to beginners covering detailed steps on how to choose the right exchange, register an account, deposit funds, and purchase Ethereum (ETH). By following this guide, beginners can safely and conveniently embark on their virtual currency investment journey and seize potential investment opportunities in 2025 and beyond.

Alipay and Bitcoin Transactions: A safe and convenient purchasing guide Alipay is a widely used payment tool in China. Although it does not support direct purchase of cryptocurrencies such as Bitcoin, you can complete transactions indirectly through some channels. This article will guide you on how to use Alipay to purchase Bitcoin safely and conveniently, and explain the risks that need to be paid attention to. The most common way to purchase Bitcoin indirectly through cryptocurrency exchanges is to trade through cryptocurrency exchanges that support Alipay. This method usually requires you to buy USDT (stablecoin) first and then exchange USDT for Bitcoin. The following is an example of Ouyi OKX Exchange, explain the operation steps in detail: Register Ouyi OKX account: Visit Ouyi OKX official website (click to register), register an account using email address, and complete

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.
