Narration:
- There are two encryption schemes on the front end, one is a reversible encryption algorithm and the other is an irreversible encryption algorithm.
DES
and AES
is reversible, so encryption and decryption can be performed by the key,
MD5
It is irreversible, and it can only be determined whether the plaintext is the same by encrypting it again.
1. MD5 front-end encryption (irreversible)
npm install --save js-md5
- Introduced in
main.js
and mounted Vue的prototype
on
import md5 from 'js-md5';
Vue.prototype.$md5 = md5;
- Encrypt when sending request
this.login({
username: that.formLogin.username,
password: that.$md5(that.formLogin.password),
captcha: that.formLogin.captcha,
captchaKey: that.captchaKey
})
2. AES/DES encryption and decryption
- Install dependency packages
npm install crypto-js
- Encapsulate encryption and decryption public methods
import CryptoJS from 'crypto-js';
export const generatekey=(num)=> {
let library = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let key = "";
for (var i = 0; i < num; i++) {
let randomPoz = Math.floor(Math.random() * library.length);
key += library.substring(randomPoz, randomPoz + 1);
}
return key;
}
export const encrypt=(word, keyStr)=> {
keyStr = keyStr ? keyStr : 'XXXXXXXXXXXXX';
var key = CryptoJS.enc.Utf8.parse(keyStr);
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();
}
export const listdecrypt=(word, keyStr)=>{
keyStr = keyStr ? keyStr : 'XXXXXXXXXXXXX';
var key = CryptoJS.enc.Utf8.parse(keyStr);
var decrypt = CryptoJS.AES.decrypt(word, key, {
mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
addAES() {
const aseKey = "12345678";
const message = "[email protected]";
this.password = encrypt(message,aseKey);
console.log(this.password);
},
addAESJIEmi() {
const aseKey = "12345678";
const data = listdecrypt(this.password,aseKey);
console.log(data);
},