You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

36 lines
903 B

  1. var crypto = require('crypto')
  2. function WXBizDataCrypt(appId, sessionKey) {
  3. this.appId = appId
  4. this.sessionKey = sessionKey
  5. }
  6. WXBizDataCrypt.prototype.decryptData = function (encryptedData, iv) {
  7. // base64 decode
  8. var sessionKey = new Buffer(this.sessionKey, 'base64')
  9. encryptedData = new Buffer(encryptedData, 'base64')
  10. iv = new Buffer(iv, 'base64')
  11. try {
  12. // 解密
  13. var decipher = crypto.createDecipheriv('aes-128-cbc', sessionKey, iv)
  14. // 设置自动 padding 为 true,删除填充补位
  15. decipher.setAutoPadding(true)
  16. var decoded = decipher.update(encryptedData, 'binary', 'utf8')
  17. decoded += decipher.final('utf8')
  18. decoded = JSON.parse(decoded)
  19. } catch (err) {
  20. throw new Error('Illegal Buffer')
  21. }
  22. if (decoded.watermark.appid !== this.appId) {
  23. throw new Error('Illegal Buffer')
  24. }
  25. return decoded
  26. }
  27. module.exports = WXBizDataCrypt