在执行
decoded += decipher.final('utf8')
的时候提示错误
(node:26500) UnhandledPromiseRejectionWarning: Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
请问是什么原因呢?
具体代码如下:
WXBizDataCrypt.prototype.decryptData = function (encryptedData, iv) {
// base64 decode
var sessionKey = Buffer.from(this.sessionKey, 'base64')
encryptedData = Buffer.from(encryptedData, 'base64')
iv = Buffer.from(iv, 'base64')
// try {
// 解密
var decipher = crypto.createDecipheriv('aes-128-cbc', sessionKey, iv)
// 设置自动 padding 为 true,删除填充补位
decipher.setAutoPadding(true)
var decoded = decipher.update(encryptedData, 'binary', 'utf8')
decoded += decipher.final('utf8')
decoded = JSON.parse(decoded)
// } catch (err) {
// throw new Error('Illegal Buffer')
// }
if (decoded.watermark.appid !== this.appId) {
throw new Error('Illegal Buffer')
}
return decoded
}
可以尝试一下
https://developers.weixin.qq.com/blogdetail?action=get_post_info&docid=000466ea294fe0c50266ac5b15b800&highline=crypto.createDecipheriv
https://mp.weixin.qq.com/wxopen/plugindevdoc?appid=wxf25d506ff81e19fb&token=200365472&lang=zh_CN