util 底下得 AES.js 文件
npm install crypto-js
import CryptoJS from 'crypto-js'
/* 判断是否加密 true加 false不加 */
const encrypt = false;
/**
* @Description: 加密
* @Author: WenBo Wei
* @Date: 2020/7/22
*
* @param data string 待加密数据字符串
* @param secret string 秘钥
* @param iv string 偏移量
* @return string 加密后字符串
*/
export function encryptData(data, secret = '9ouxQY9ALwFhQaFj', iv = 'FQFBCcQh59HNFr2M') { // 加密
let $secret = CryptoJS.enc.Utf8.parse(secret)
let $iv = CryptoJS.enc.Utf8.parse(iv)
let encrypted = CryptoJS.AES.encrypt(data, $secret, {
iv: $iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
encrypted = encrypted.toString() // 返回的是base64格式的密文
return encrypted
}
/**
* @Description: 解密
* @Author: WenBo Wei
* @Date: 2020/7/22
*
* @param data string 待解密数据字符串
* @param secret string 秘钥
* @param iv string 偏移量
* @return string 解密后字符串
*/
export function decryptData(data, secret = '9ouxQY9ALwFhQaFj', iv = 'FQFBCcQh59HNFr2M') {
let $secret = CryptoJS.enc.Utf8.parse(secret);
let $iv = CryptoJS.enc.Utf8.parse(iv);
let decrypted = CryptoJS.AES.decrypt(data, $secret, {
iv: $iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
/*
加密数据
*/
export function aesEncrypt(req) {
if (encrypt) {
let secret = wx.getStorageSync('secret')
if (secret){
secret = secret.slice(0,16)
}
let timestamp = ~~((new Date()).getTime() / 1000);
let encrypt_data
if (req.method === 'post') {
if (!req.data) req.data = {}
req.data['timestamp'] = timestamp
if (secret) {
encrypt_data = encryptData(JSON.stringify(req.data), secret)
} else {
encrypt_data = encryptData(JSON.stringify(req.data))
}
req.data = {
data: encrypt_data
}
} else {
if (!req.params) req.params = {}
req.params['timestamp'] = timestamp
if (secret) {
encrypt_data = encryptData(JSON.stringify(req.params), secret)
} else {
encrypt_data = encryptData(JSON.stringify(req.params))
}
req.params = {
data: encrypt_data
}
}
}
return req
}
/*
解密数据
*/
export function aesDecrypt(res) {
if (encrypt) {
let secret = wx.getStorageSync('secret')
if (secret){
secret = secret.slice(0,16)
}
let decrypt_data
if (secret) {
decrypt_data = decryptData(res.data.data, secret)
} else {
decrypt_data = decryptData(res.data.data)
}
res.data.data = JSON.parse(decrypt_data)
}
return res
}
api-manager.js 中引用加密/解密函数
import {
aesEncrypt,
aesDecrypt
} from './AES.js'
//在请求拦截器中讲数据放到解密函数中
const req_data = aesEncrypt(request)
console.log('%c请求拦截器:', 'color:#ffa502;font-size:20px;font-weight:bold;',req_data);
//响应拦截器
const res_data = aesDecrypt(response)
console.log('%c响应拦截器:', 'color:green;font-size:20px;font-weight:bold;',res_data);
网友评论