装置
npm install crypto-js –save
unit.js
import CryptoJS from "crypto-js";
//秘钥
const CRYPTOJSKEY= "abcdefghighkml";
export default {
//加密
/*
* {param} plaintText 加密明文
* return str 加密效果
*/
encrypt(plaintText) {
var plaintText = plaintText;
var options = {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
};
var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
var encryptedData = CryptoJS.AES.encrypt(plaintText, key, options);
var encryptedBase64Str = encryptedData.toString();
return encryptedBase64Str;
},
//解密
/*
* {param} plaintText 解密密文
* return str 解密效果
*/
decrypt(encryptedBase64Str, type) {
var encryptedBase64Str = encryptedBase64Str;
var options = {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
};
var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
// 解密
var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, options);
// 解密后,须要根据Utf8的体式格局将明文转位字符串
var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
return decryptedStr;
}
};
运用
//引入
import Util from "util";
//加密
let userPwd = Util.encrypt('123456');