前端信息安全处理过程中,要与后端接口进行加密传参,例如登陆注册修改密码等等;
AES加密
安装:
npm install crypto-js
封装:
import cryptoJs from 'crypto-js';
export default {
data() {
return {}
},
methods: {
// 随机生成指定数量的16进制key
generatekey(NUM) {
let library = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let key = "";
for (let i = 0; i < num; i++) {
let randomPoz = Math.floor(Math.random() * library.length);
key += library.substring(randomPoz, randomPoz + 1);
}
return key;
},
// DES加密
encryptDes (message, key) {
let keyHex = cryptoJs.enc.Utf8.parse(key)
let option = {
mode: cryptoJs.mode.ECB,
padding: cryptoJs.pad.Pkcs7
}
let encrypted = cryptoJs.DES.encrypt(message, keyHex, option)
return encrypted.ciphertext.toString()
},
// DES解密
decryptDes (message, key) {
let keyHex = cryptoJs.enc.Utf8.parse(key)
let decrypted = cryptoJs.DES.decrypt({
ciphertext: cryptoJs.enc.Hex.parse(message)
},
keyHex, {
mode: cryptoJs.mode.ECB,
padding: cryptoJs.pad.Pkcs7
}
)
return decrypted.toString(cryptoJs.enc.Utf8)
},
// 加密
encryptPassword (password) {
let keys = 'Y6IlWdeju7byCywQ' // Y6IlWdeju7byCywQ generatekey(16)
let newPwd = this.encryptDes(password, keys)
return newPwd
}
}
}
使用:
password: this.encryptPassword(this.password), // 加密 DES.js
示例: