1.安装cryptojs
npm i crypto-js
2.封装cryptojs
/* eslint-disable */
// const cryptoJs = require('@/utils/crypto-js')
import cryptoJs from 'crypto-js'
const key = "1"
const iv = "1"
const d = {
/**
* encryptByDES 加密
*/
encryptByDES(message) {
const keyHex = cryptoJs.enc.Utf8.parse(key);
const encrypted = cryptoJs.TripleDES.encrypt(message, keyHex, {
iv: cryptoJs.enc.Utf8.parse(iv),
mode: cryptoJs.mode.CBC,
padding: cryptoJs.pad.Pkcs7
});
return encrypted.toString();
},
/**
* decryptByDES 解密
*/
decryptByDES(ciphertext) {
const keyHex = cryptoJs.enc.Utf8.parse(key);
const decrypted = cryptoJs.TripleDES.decrypt({
ciphertext: cryptoJs.enc.Base64.parse(ciphertext)
}, keyHex, {
iv: cryptoJs.enc.Utf8.parse(iv),
mode: cryptoJs.mode.CBC,
padding: cryptoJs.pad.Pkcs7
});
return decrypted.toString(cryptoJs.enc.Utf8);
}
}
export default d
3.使用cryptojs
先引入封装好的js文件
import d from '...'
getData(){
getCertificateInfoList(d.encryptByDES(JSON.stringify({state:2}))).then(res=>{
console.log(d.decryptByDES(res.data.list))
})
},
ps:或者可以在vue拦截的时候进行加密解密