安装加解密插件crypto-js
cnpm install crypto-js
新建一个js(encryption.js)用来放置加密解密的方法
import CryptoJS from 'crypto-js';
//DES加密
export const encryptBy = (message) => {
var key = '12345678';
function encryptByDES (message, key) {
var keyHex = CryptoJS.enc.Utf8.parse(key)
var option = {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7}
var encrypted = CryptoJS.DES.encrypt(message, keyHex, option)
return encrypted.ciphertext.toString()
}
return encryptByDES(message, key);
}
//DES解密
export const decryptBy = (message) => {
var key = '12345678';
//DES ECB模式解密
function decryptByDES(message,key){
var keyHex = CryptoJS.enc.Utf8.parse(key);
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(message)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
var result_value = decrypted.toString(CryptoJS.enc.Utf8);
return result_value;
}
return decryptByDES(message, key);
}
在页面中使用
一、在vue页面中引用
引入js
import {encryptBy,decryptBy} from '@/view/api/encryption';
加密解密
console.log(encryptBy('123456'));//加密
console.log(decryptBy('103e702e0737327c'));//解密
二、直接在请求接口里加密
import {encryptBy,decryptBy} from './function.js';
export function login (data) {
return request({
url: '/api/login?username=' + data.username + '&password=' + encryptBy(data.password)
method: 'post'
})
}
来源:CSDN
作者:小可爱的博客
链接:https://blog.csdn.net/weixin_45298700/article/details/103854424