/**
* 解密回调敏感数据(新版回调 ciphertext 加密)
* @param string $ciphertext 加密的回调数据
* @param string $nonce 随机串
* @param string $associatedData 附加数据
* @return array 解密后的回调数据
* @throws \Exception
*/
public function decryptNotifyData($ciphertext, $nonce, $associatedData): array
{
$apiV3Key = $this->config['api_v3_key'];
if (strlen($apiV3Key) !== 32) {
throw new \Exception("API v3 密钥必须是32位字符串");
}
$ciphertext = base64_decode($ciphertext);
$authTagLength = 16; // GCM认证标签固定16字节
if (strlen($ciphertext) < $authTagLength) {
throw new Exception('密文长度不足');
}
$encryptedData = substr($ciphertext, 0, -$authTagLength);
$authTag = substr($ciphertext, -$authTagLength);
// 使用openssl解密
$decrypted = openssl_decrypt(
$encryptedData,
'aes-256-gcm',
$apiV3Key,
OPENSSL_RAW_DATA,
$nonce,
$authTag,
$associatedData
);
if (!$decrypted) {
throw new \Exception("回调数据解密失败");
}
return json_decode($decrypted, true) ?: [];
}
以上为使用的解密方法 确定apiV3key是正确的
