WXBizDataCrypt.js 999 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Created by rd on 2017/5/4.
  3. */
  4. // 引入CryptoJS 路径依个人导入情况变动
  5. var Crypto = require('./cryptojs-master/cryptojs.js').Crypto;
  6. var app = getApp();
  7. function RdWXBizDataCrypt(appId, sessionKey) {
  8. this.appId = appId
  9. this.sessionKey = sessionKey
  10. }
  11. RdWXBizDataCrypt.prototype.decryptData = function(encryptedData, iv, appId, sessionKey) {
  12. // base64 decode :使用 CryptoJS 中 Crypto.util.base64ToBytes()进行 base64解码
  13. var encryptedData = Crypto.util.base64ToBytes(encryptedData)
  14. var key = Crypto.util.base64ToBytes(sessionKey);
  15. var iv = Crypto.util.base64ToBytes(iv);
  16. // 对称解密使用的算法为 AES-128-CBC,数据采用PKCS#7填充
  17. var mode = new Crypto.mode.CBC(Crypto.pad.pkcs7);
  18. try {
  19. // 解密
  20. var bytes = Crypto.AES.decrypt(encryptedData, key, {
  21. asBpytes: true,
  22. iv: iv,
  23. mode: mode
  24. });
  25. var decryptResult = JSON.parse(bytes);
  26. } catch (err) {
  27. console.log(err)
  28. }
  29. return decryptResult
  30. }
  31. module.exports = RdWXBizDataCrypt