小程序
小游戏
企业微信
微信支付
扫描小程序码分享
回调验签可以,但是解密ciphertext一直不成功,ciphertext 原封不动base64解码后乱码怎么是乱码,AesGcm::decrypt解密一直失败,提示Decrypting the input $ciphertext failed, please checking your $key and $iv whether or nor correct.
2 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
检查下v3密钥和商户号是否匹配。修改密钥后,需要重新下单支付,才会用新的密钥加密回调
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
直接去解密
const tag = ciphertextWithTag.slice(ciphertextWithTag.length - 16); // 最后的16字节是GCM标签
// 解析附加数据,如果有的话
const associatedData = associatedDataInput ? new Uint8Array(atob(associatedDataInput).split('').map(c => c.charCodeAt(0))) : new Uint8Array(0);
// 打印调试信息:检查密钥、nonce、密文、标签和附加数据的长度
console.log('密钥长度:', key.length);
console.log('Nonce 长度:', nonce.length);
console.log('密文长度:', ciphertext.length);
console.log('标签长度:', tag.length);
console.log('附加数据长度:', associatedData.length);
// 使用 Web Crypto API 进行解密
const decryptedData = await window.crypto.subtle.decrypt(
{
name: 'AES-GCM',
iv: nonce,
additionalData: associatedData,
tagLength: 128, // GCM标签长度,通常为128位
},
await window.crypto.subtle.importKey(
'raw', key, { name: 'AES-GCM' }, false, ['decrypt']
),
new Uint8Array([...ciphertext, ...tag]) // 拼接密文和标签
);
const decoder = new TextDecoder();
const plaintext = decoder.decode(decryptedData);
document.getElementById('result').textContent = plaintext;
} catch (err) {
// 记录更多错误信息
console.error('解密失败:', err);
document.getElementById('result').textContent = `解密失败: ${err.name} - ${err.message}`;
}
</script>
</body>
</html>
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
检查下v3密钥和商户号是否匹配。修改密钥后,需要重新下单支付,才会用新的密钥加密回调
直接去解密
const tag = ciphertextWithTag.slice(ciphertextWithTag.length - 16); // 最后的16字节是GCM标签
// 解析附加数据,如果有的话
const associatedData = associatedDataInput ? new Uint8Array(atob(associatedDataInput).split('').map(c => c.charCodeAt(0))) : new Uint8Array(0);
// 打印调试信息:检查密钥、nonce、密文、标签和附加数据的长度
console.log('密钥长度:', key.length);
console.log('Nonce 长度:', nonce.length);
console.log('密文长度:', ciphertext.length);
console.log('标签长度:', tag.length);
console.log('附加数据长度:', associatedData.length);
// 使用 Web Crypto API 进行解密
const decryptedData = await window.crypto.subtle.decrypt(
{
name: 'AES-GCM',
iv: nonce,
additionalData: associatedData,
tagLength: 128, // GCM标签长度,通常为128位
},
await window.crypto.subtle.importKey(
'raw', key, { name: 'AES-GCM' }, false, ['decrypt']
),
new Uint8Array([...ciphertext, ...tag]) // 拼接密文和标签
);
const decoder = new TextDecoder();
const plaintext = decoder.decode(decryptedData);
document.getElementById('result').textContent = plaintext;
} catch (err) {
// 记录更多错误信息
console.error('解密失败:', err);
document.getElementById('result').textContent = `解密失败: ${err.name} - ${err.message}`;
}
}
</script>
</body>
</html>