1、md5加密:前端可以加密,不可以解密
2、crypto加密: 前端可加密、解密常用于“记住密码”, 存cookie
import CryptoJS from "crypto-js";
//设置cookie
setCookie(portId, psw, exdays) {
// Encrypt,加密账号密码
const userName = CryptoJS.AES.encrypt(
portId+'',
"secretkey123"
).toString();
let cipherPsw = CryptoJS.AES.encrypt(psw+'', "secretkey123").toString();
// window.console.log(userName+'/'+cipherPsw)//打印一下看看有没有加密成功
let exdate = new Date(); //获取时间
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
//字符串拼接cookie,为什么这里用了==,因为加密后的字符串也有个=号,影响下面getcookie的字符串切割,你也可以使用更炫酷的符号。
window.document.cookie =
"userName" +
"==" +
userName +
";path=/;expires=" +
exdate.toGMTString();
window.document.cookie =
"password" +
"==" +
cipherPsw +
";path=/;expires=" +
exdate.toGMTString();
},
//读取cookie
getCookie () {
if (document.cookie && document.cookie.length > 0) {
let arr = document.cookie.split("; "); //这里显示的格式请根据自己的代码更改
for (let i = 0; i < arr.length; i++) {
let arr2 = arr[i].split("=="); //根据==切割
//判断查找相对应的值
if (arr2[0] == "userName") {
// Decrypt,将解密后的内容赋值给账号
let bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey123");
this.userName = bytes.toString(CryptoJS.enc.Utf8) || '';
} else if (arr2[0] == "password") {
// Decrypt,将解密后的内容赋值给密码
let bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey123");
this.password = bytes.toString(CryptoJS.enc.Utf8) || '';
}
}
}
},