PHP解密支付宝小程序的加密数据,手机号等。
阅读原文时间:2023年07月09日阅读:1

1.小程序端代码示例

my.getPhoneNumber({
success: (res) => {
let encryptedData = res.response;
my.httpRequest({
url: '你的后端服务端',
data: encryptedData,
});
},
fail: (res) => {
console.log(res);
console.log('getPhoneNumber_fail');
},
});

2.PHP后端解密示例

public static function decryptData($encryptedData, $key = '开发设置-接口内容加密方式-查看-字符串')
{
$encrys = json_decode($encryptedData, true);
$encryptedData = $encrys['response'];
$str = base64_decode($encryptedData);
$screct_key = base64_decode($key);

    //设置全0的IV  
    $iv\_size = mcrypt\_get\_iv\_size(MCRYPT\_RIJNDAEL\_128, MCRYPT\_MODE\_CBC);  
    $iv = str\_repeat("\\0", $iv\_size);

    $decrypt\_str = mcrypt\_decrypt(MCRYPT\_RIJNDAEL\_128, $screct\_key, $str, MCRYPT\_MODE\_CBC, $iv);  
    $decrypt\_str = self::stripPKSC7Padding($decrypt\_str);  
    return $decrypt\_str;  
}

public static function stripPKSC7Padding($source)
{
$char = substr($source, -1);
$num = ord($char);
if ($num == 62) return $source;
$source = substr($source, 0, -$num);
return $source;
}

3.解密返回

{"code":"10000","msg":"Success","mobile":"185xxxxx111"}