http通信加解密

soul2/encrypt
soul2 1 year ago
parent d2f010d6f1
commit 2e29e365e6
  1. 1
      package.json
  2. 67
      src/utils/aes.js
  3. 9
      src/utils/request.js
  4. 2
      src/views/login/index.vue

@ -16,6 +16,7 @@
"dependencies": {
"axios": "0.18.1",
"core-js": "3.6.5",
"crypto-js": "^4.2.0",
"element-ui": "2.15.6",
"js-cookie": "2.2.0",
"normalize.css": "7.0.0",

@ -0,0 +1,67 @@
/**
* Aes加解密
* aec1b4218112932c
* b0e8d4dbaff1ed30
* ec241d54487cabf8
* f768a3dc18069961
* 620b8d3c3be1e725
*/
const aesKey = '620b8d3c3be1e725'
const crypto = require('crypto-js')
const key = crypto.enc.Utf8.parse(aesKey)
/**
* 加密解密工具类
*/
function encrypt(word) {
var srcs = crypto.enc.Utf8.parse(word)
var encrypted = crypto.AES.encrypt(srcs, key, {
mode: crypto.mode.ECB,
padding: crypto.pad.Pkcs7
})
return encrypted.toString()
// return strToHex(encrypted.toString());
}
function decrypt(word) {
//word = hexToStr(word);
var decrypt = crypto.AES.decrypt(word, key, {
mode: crypto.mode.ECB,
padding: crypto.pad.Pkcs7
})
return crypto.enc.Utf8.stringify(decrypt).toString()
}
function strToHex(str) {
var val = ''
for (var i = 0; i < str.length; i++) {
if (val == '') {
val = str.charCodeAt(i).toString(16)
} else {
val += str.charCodeAt(i).toString(16)
}
}
val += '0a'
return val
}
function hexToStr(hex) {
debugger;
//hex = JSON.stringify(hex);
hex = hex + ''
var arr = hex.split('')
var out = ''
for (var i = 0; i < arr.length / 2; i++) {
var tmp = '0x' + arr[i * 2] + arr[i * 2 + 1]
var charValue = String.fromCharCode(tmp)
out += charValue
}
return out
}
export default {
decrypt,
encrypt,
}

@ -2,6 +2,7 @@ import axios from 'axios'
import { Message, MessageBox } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'
import aes from '@/utils/aes'
// create an axios instance
const service = axios.create({
@ -18,6 +19,12 @@ const service = axios.create({
service.interceptors.request.use(
config => {
// do something before request is sent
// 对post请求进行加密
if (config.data && config.method === 'post') {
console.log('原始数据 -> ', JSON.stringify(config.data))
config.data = aes.encrypt(JSON.stringify(config.data))
console.log('加密为 -> ', config.data)
}
if (store.getters.token) {
// let each request carry token
@ -48,7 +55,7 @@ service.interceptors.response.use(
*/
response => {
const res = response.data
console.log('response -> ', response)
// if the custom code is not 0, it is judged as an error.
if (res.code !== 0) {
Message({

@ -64,7 +64,7 @@ export default {
name: 'Login',
data() {
const validateUsername = (rule, value, callback) => {
if (false || !validUsername(value)) {
if (!validUsername(value)) {
callback(new Error('Please enter the correct user name'))
} else {
callback()

Loading…
Cancel
Save