aes.js 1.1 KB

123456789101112131415161718192021222324
  1. // import * as w from './node_modules/_crypto-js@4.1.1@crypto-js/crypto-js.js'
  2. var CryptoJS = require("./node_modules/_crypto-js@4.1.1@crypto-js/crypto-js.js");
  3. // import aa from 'common.js'
  4. export default {
  5. // 加密(ECB模式)
  6. encrypt(word, keyStr) { // word, keyStr第一个参数是加密的字段名字 第二个是key值(16位)
  7. keyStr = keyStr || 'abcdefG1234567' // 密文(密钥)
  8. let key = CryptoJS.enc.Utf8.parse(keyStr)
  9. let src = CryptoJS.enc.Utf8.parse(word)
  10. var encrypted = CryptoJS.AES.encrypt(src, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7})
  11. //return encrypted.toString()
  12. return CryptoJS.enc.Base64.stringify(encrypted)
  13. },
  14. // 解密(ECB模式)
  15. decrypt(word, keyStr) {
  16. keyStr = keyStr || 'abcdefG1234567'
  17. let base64 = CryptoJS.enc.Base64.parse(word);
  18. let src = CryptoJS.enc.Base64.stringify(base64);
  19. let key = CryptoJS.enc.Utf8.parse(keyStr)
  20. let decrypt = CryptoJS.AES.decrypt(src, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7})
  21. return CryptoJS.enc.Utf8.stringify(decrypt).toString()
  22. }
  23. }