微信支付成功后,回调给的ciphertext,associated_data,nonce,然后拿着三个参数进行解密,
function decryptData(ciphertext, nonce, associatedData) {
const key = Buffer.from(merchantApiKey, 'utf-8');
if (key.length !== 32) {
throw new Error('密钥长度必须为 32 字节');
}
const decipher = crypto.createDecipheriv('aes-256-gcm', key, Buffer.from(nonce, 'utf-8'));
decipher.setAAD(Buffer.from(associatedData, 'utf-8'));
let decrypted = '';
try {
decrypted += decipher.update(Buffer.from(ciphertext, 'base64'), undefined, 'utf-8');
decrypted += decipher.final('utf-8');
} catch (err) {
console.error('解密失败:', err.message);
return null; // 或处理错误
}
return decrypted; // 返回解密后的字符串
}
一直报错,所有参数都是 微信返回的,apiv3也确认是正确的,目前不知道问题出现在哪,报错内容为Unsupported state or unable to authenticate data
要么key不对,要么输入数据的编码和格式不对,可以用https://npm.runkit.com/wechatpay-axios-plugin 这个验证一下