最简单的加密方式,AES

创建utls.js文件

import Vue from 'vue'
import CryptoJS from 'crypto-js'
export default {//加密
  encrypt(word, keyStr){
    keyStr = keyStr ? keyStr : 'dsadjsahdklsahdjsakhdsak';   //abcdefgabcdefg121978925762
    var key  = CryptoJS.enc.Utf8.parse(keyStr);//Latin1 w8m31+Yy/Nw6thPsMpO5fg==
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
    return encrypted.toString();
  },
  //解密
  decrypt(word, keyStr){
    keyStr = keyStr ? keyStr : 'dsadjsahdklsahdjsakhdsak';  //abcdefgabcdefg121978925762
    var key  = CryptoJS.enc.Utf8.parse(keyStr);//Latin1 w8m31+Yy/Nw6thPsMpO5fg==
    var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
  }

}

需要加密和解密的页面

import Utils from "./assets/js/utils";
var arr = Utils.encrypt(JSON.stringify(this.ruleForm),"hAw6eqnFLKxpsDv3"); //AES加密

var all = Utils.decrypt(account, "hAw6eqnFLKxpsDv3"); //AES解密
var obj = eval("(" + all + ")");
console.log(obj);

这样就实现了加密和解密

你可能感兴趣的:(vue.js)