上码

	let CryptoJS = require('crypto-js')
	
	export const aesUtil = {
	
	  //获取key,如果在不需要解密的情况可以直接使用这个获取key
	  genKey: function (length = 16) {
	    let random = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	    let str = "";
	    for (let i = 0; i < length; i++) {
	      str = str + random.charAt(Math.random() * random.length)
	    }
	    return str;
	  },
	
	  //加密
	  encrypt: function (plaintext, key = 'MIGfMA0GCSqGSIb3') {
	    if (plaintext instanceof Object) {
	      plaintext = JSON.stringify(plaintext)
	    }
	    let encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(plaintext), CryptoJS.enc.Utf8.parse(key), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
	    return encrypted.toString();
	  },
	
	  //解密
	  decrypt: function (ciphertext, key = 'MIGfMA0GCSqGSIb3') {
	    try {
	      let decrypt = CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(key), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
	      let decString = CryptoJS.enc.Utf8.stringify(decrypt).toString();
	      if (decString.charAt(0) === "{" || decString.charAt(0) === "[") {
	        decString = JSON.parse(decString);
	      }
	      return decString;
	    } catch (err) {
	      return {msg:'解密失败'}
	    }
	  }
	};