1. The content of $prestr is the same as MD5 (see the manual, but does not include the final MD5 password)
2. Merchant private key for signature
3. The final signature needs to be encoded in base64
4. The value returned by this function is the RSA signature of this request.
1
2 /**
3 * Verify signature
4 * @param $prestr String that needs to be signed
5 * @param $sign signature result
6 * return signature result
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 ?>
Copy code
Note:
1. The content of $prestr is the same as MD5 (see manual)
2.$sign is the binary decoded by base64_decode of the sign parameter returned by the Alipay interface
3. Use Alipay public key for signature verification
4. This function returns a Boolean value, directly telling you whether the signature verification passed
The PHP SDK demo officially provided by Alipay only handles the MD5 encryption method. However, when the Android and iOS terminals request the Alipay encryption method, they can only use the RSA encryption algorithm. At this time, the server-side PHP cannot verify the signature, so Some modifications need to be made to the demo.
1. Modify the alipay_notify.class.php file
verifyNotify function line 46
$isSign = $this->getSignVeryfy($_POST, $_POST["sign"]);
changed to
$isSign = $this->getSignVeryfy($_POST, $_POST["sign"], $_POST["sign_type"]);
verifyReturn function line 83
$isSign = $this->getSignVeryfy($_GET, $_GET["sign"]);
changed to
$isSign = $this->getSignVeryfy($_GET, $_GET["sign"], $_GET["sign_type"]);
getSignVeryfy function line 116
function getSignVeryfy($para_temp, $sign) {
changed to
function getSignVeryfy($para_temp, $sign, $sign_type) {
getSignVeryfy function line 127
switch (strtoupper(trim($this->alipay_config['sign_type']))) {
case "MD5" :
$isSgin = md5Verify($prestr, $sign, $this->alipay_config['key']);
break;
default :
$isSgin = false;
}
changed to
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. Create a new alipay_rsa.function.php file
Copy code
1
2 /* *
3 * RSA
4 * Details: RSA encryption
5 * Version: 3.3
6 * Date: 2014-02-20
7 * Description:
8 * The following code is only a sample code provided to facilitate merchant testing. Merchant can write it according to the technical documentation according to the needs of their own website. It is not necessary to use this code.
9 * This code is only for learning and researching the Alipay interface and is only provided as a reference.
10 */
11 /**
12 * Signature string
13 * @param $prestr String that needs to be signed
14 * return signature result
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 * Verify signature
26 * @param $prestr String that needs to be signed
27 * @param $sign signature result
28 * return signature result
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 ?>
Copy code
The last thing I want to say is that the official manual is basically correct, but there are some places that are not very detailed. You must refer to it more when developing. That's roughly it. I wish everyone good luck.
http://www.bkjia.com/PHPjc/735876.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735876.htmlTechArticleThe PHP RSA signature verification problem that has been bothering these two days has finally been solved. Since I didn’t have much contact with RSA before, In addition, there is no official PHP SDK for reference yet, so I took some detours and wrote...