随机密码加密解密--v2环境,不传signature解密不出来问题

This commit is contained in:
liyuetong
2021-12-15 17:47:14 +08:00
parent 1828c014da
commit 269bf1596f
3 changed files with 81 additions and 28 deletions

View File

@@ -0,0 +1,47 @@
import CryptoJS from 'crypto-js'
export default {
AESEncrypt: function(data, keys) {
//加密
var key = CryptoJS.enc.Hex.parse(keys)
var iv = CryptoJS.enc.Utf8.parse('0123456789ABCDEF') //偏移量
var encrypted = CryptoJS.AES.encrypt(data, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
return encrypted.toString()
},
AESDecrypt: function(encrypted, keys) {
//解密
var key = CryptoJS.enc.Hex.parse(keys)
var iv = CryptoJS.enc.Utf8.parse('0123456789ABCDEF')
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
return CryptoJS.enc.Utf8.stringify(decrypted)
},
AESCacheEncrypt: function(data, keys) {
//加密 緩存
var key = CryptoJS.enc.Hex.parse(keys)
var iv = CryptoJS.enc.Utf8.parse('0123456789ABCDEF') //偏移量
var encrypted = CryptoJS.AES.encrypt(data, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
return encrypted.toString()
},
AESCacheDecrypt: function(encrypted, keys) {
//解密 緩存
var key = CryptoJS.enc.Hex.parse(keys)
var iv = CryptoJS.enc.Utf8.parse('0123456789ABCDEF')
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
return CryptoJS.enc.Utf8.stringify(decrypted)
}
}

View File

@@ -1,5 +1,5 @@
import axios from 'axios' import axios from 'axios'
// import AESTools from '@/assets/js/utils/cryptoJsUtil' import AESToolsV2 from '@/assets/js/utils/cryptoJsUtilV2'
import { import {
MD5, MD5,
RSAEncrypt, RSAEncrypt,
@@ -155,14 +155,18 @@ service.interceptors.request.use(
message: '加载中……' message: '加载中……'
}) })
} }
// 生成本次请求随机密钥
let masterKey let masterKey
if (!window.localStorage.getItem('masterKey')) { if(configApp.API_VERSION == 'v3'){
masterKey = randomString(); // 生成本次请求随机密钥
window.localStorage.setItem('masterKey',masterKey) if (!window.localStorage.getItem('masterKey')) {
} else { masterKey = randomString();
masterKey = window.localStorage.getItem('masterKey') window.localStorage.setItem('masterKey',masterKey)
} else {
masterKey = window.localStorage.getItem('masterKey')
}
} }
// token 不存在初始化处理
let token = CacheUtils.getLocItem('token')
/** /**
* 请求拦截处理(待添加 判断走统一网关处理) * 请求拦截处理(待添加 判断走统一网关处理)
*/ */
@@ -195,15 +199,13 @@ service.interceptors.request.use(
console.log(AESDecrypt(config.data.data, masterKey)); console.log(AESDecrypt(config.data.data, masterKey));
} }
}else if(configApp.API_VERSION == 'v2'){ }else if(configApp.API_VERSION == 'v2'){
let encrypt = AESEncrypt(JSON.stringify(config.data), configApp.REQ_PWD) config.headers['signature'] = MD5(timeStr + CacheUtils.getLocItem('token'))
let encrypt = AESToolsV2.AESEncrypt(JSON.stringify(config.data), configApp.REQ_PWD)
config.data = { data: encrypt } config.data = { data: encrypt }
} }
// config.data = { data: encrypt }
} }
} }
// token 不存在初始化处理
let token = CacheUtils.getLocItem('token')
if (!token) { if (!token) {
console.log('token====>>', token) console.log('token====>>', token)
// 设备类型 // 设备类型
@@ -229,7 +231,6 @@ service.interceptors.request.use(
// 添加请时间戳 // 添加请时间戳
let timeStr = new Date().getTime() + '' let timeStr = new Date().getTime() + ''
config.headers['timeStr'] = timeStr config.headers['timeStr'] = timeStr
// config.headers['signature'] = MD5(timeStr + CacheUtils.getLocItem('token'))
return config return config
}, },
error => { error => {
@@ -249,20 +250,25 @@ service.interceptors.response.use(
/api\/$/.test(response.config.url.split(configApp.API_VERSION)[0]) /api\/$/.test(response.config.url.split(configApp.API_VERSION)[0])
) { ) {
if (res.response) { if (res.response) {
// 正常情況返回必有response 节点 if(configApp.API_VERSION == 'v3'){
// 返回结果的随机密钥 // 正常情況返回必有response 节点
let masterKey; // 返回结果的随机密钥
if (!window.localStorage.getItem('masterKey')) { let masterKey;
return Promise.reject('密钥失效') if (!window.localStorage.getItem('masterKey')) {
} else { return Promise.reject('密钥失效')
masterKey = window.localStorage.getItem('masterKey'); } else {
} masterKey = window.localStorage.getItem('masterKey');
console.log('请求结果response' + response) }
console.log('请求结果masterKey' + masterKey) console.log('请求结果response' + response)
console.log(JSON.parse(AESDecrypt(response.data.response, masterKey))) console.log('请求结果masterKey' + masterKey)
// 使用随机密钥解密返回结果data console.log(JSON.parse(AESDecrypt(response.data.response, masterKey)))
res = JSON.parse(AESDecrypt(res.response, masterKey)) // 使用随机密钥解密返回结果data
// res = JSON.parse(AESTools.AESDecrypt(res.response, configApp.REQ_PWD)) res = JSON.parse(AESDecrypt(res.response, masterKey))
}else if(configApp.API_VERSION == 'v2'){
// 使用固定密钥解密返回结果data
res = JSON.parse(AESToolsV2.AESDecrypt(res.response, configApp.REQ_PWD))
console.log(JSON.parse(AESToolsV2.AESDecrypt(res.response, configApp.REQ_PWD)),'JSON.parse(AESDecrypt(res.response, configApp.REQ_PWD))')
}
} }
} }

View File

@@ -19,7 +19,7 @@ let apiDomain, imgDomain, assetsUrl, mainUrl, payUrl, zssqUrl, REQ_PWD, CACHE_EN
console.log('环境:', process.env.VUE_APP_FLAG) console.log('环境:', process.env.VUE_APP_FLAG)
switch (process.env.VUE_APP_FLAG) { switch (process.env.VUE_APP_FLAG) {
case 'dev': case 'dev':
apiDomain = 'https://iagentsales-test2.e-guofu.com:5200/api/v1' // 国富api ///api/v1 apiDomain = 'https://iagentsales-test2.e-guofu.com:5200/api/v2' // 国富api ///api/v1
imgDomain = 'https://iagentsales-test3.e-guofu.com:5443/updown' // dev imgDomain = 'https://iagentsales-test3.e-guofu.com:5443/updown' // dev
// 静态服务资源 // 静态服务资源
assetsUrl = 'https://iagentsales-test2.e-guofu.com:443/app/' assetsUrl = 'https://iagentsales-test2.e-guofu.com:443/app/'
@@ -31,7 +31,7 @@ switch (process.env.VUE_APP_FLAG) {
// zssqUrl = 'https://iagentsales-test2.e-guofu.com:5100/html/test/index.html#/' // zssqUrl = 'https://iagentsales-test2.e-guofu.com:5100/html/test/index.html#/'
REQ_PWD = '41424344454631323334353637383930' REQ_PWD = '41424344454631323334353637383930'
CACHE_ENCRYP = '41424344454631323334353637383930' CACHE_ENCRYP = '41424344454631323334353637383930'
API_VERSION = 'v1' API_VERSION = 'v2'
break break
case 'uat': case 'uat':
apiDomain = 'https://iagentsales-test2.e-guofu.com:5200/api/v1' // 国富api apiDomain = 'https://iagentsales-test2.e-guofu.com:5200/api/v1' // 国富api