随机密钥加密解密需求,区分v3随机密钥加密,v2固定密钥加密

This commit is contained in:
liyuetong
2021-12-09 15:40:15 +08:00
committed by li.yuetong
parent 9cdedf9017
commit 8d14ba6b55

View File

@@ -155,13 +155,15 @@ service.interceptors.request.use(
message: '加载中……'
})
}
// 生成本次请求随机密钥
let masterKey
if (!window.localStorage.getItem('masterKey')) {
masterKey = randomString();
window.localStorage.setItem('masterKey',masterKey)
} else {
masterKey = window.localStorage.getItem('masterKey')
if(configApp.API_VERSION == 'v3'){
// 生成本次请求随机密钥
if (!window.localStorage.getItem('masterKey')) {
masterKey = randomString();
window.localStorage.setItem('masterKey',masterKey)
} else {
masterKey = window.localStorage.getItem('masterKey')
}
}
/**
* 请求拦截处理(待添加 判断走统一网关处理)
@@ -172,25 +174,28 @@ service.interceptors.request.use(
config.data = {}
}
if (!!config.data && config.data != null) {
// let encrypt = AESTools.AESEncrypt(JSON.stringify(config.data), configApp.REQ_PWD)
console.log('随机密钥:' + masterKey);
// RSA加密随机密钥
let ak = RSAEncrypt(masterKey, publicKey);
console.log('加密后的masterKey' + ak);
// 随机密钥加密data
let data = AESEncrypt(JSON.stringify(config.data), masterKey);
console.log('MD5值' + MD5(AESEncrypt(data, masterKey)).toString());
// 验证数据完整性MD5
// 数据完整性MD5需要进行二次AES加密
config.headers['signature'] = MD5(AESEncrypt(data, masterKey)).toString();
config.data = { ak, data };
console.log(config.data);
if(AESDecrypt(config.data.data, masterKey)){
console.log(JSON.parse(AESDecrypt(config.data.data, masterKey)));
}else{
console.log(AESDecrypt(config.data.data, masterKey));
if(configApp.API_VERSION == 'v3'){
console.log('随机密钥:' + masterKey);
// RSA加密随机密钥
let ak = RSAEncrypt(masterKey, publicKey);
console.log('加密后的masterKey' + ak);
// 随机密钥加密data
let data = AESEncrypt(JSON.stringify(config.data), masterKey);
console.log('MD5值' + MD5(AESEncrypt(data, masterKey)).toString());
// 验证数据完整性MD5
// 数据完整性MD5需要进行二次AES加密
config.headers['signature'] = MD5(AESEncrypt(data, masterKey)).toString();
config.data = { ak, data };
console.log(config.data);
if(AESDecrypt(config.data.data, masterKey)){
console.log(JSON.parse(AESDecrypt(config.data.data, masterKey)));
}else{
console.log(AESDecrypt(config.data.data, masterKey));
}
}else if(configApp.API_VERSION == 'v2'){
let encrypt = AESEncrypt(JSON.stringify(config.data), configApp.REQ_PWD)
config.data = { data: encrypt }
}
// config.data = { data: encrypt }
}
}
@@ -241,20 +246,24 @@ service.interceptors.response.use(
/api\/$/.test(response.config.url.split(configApp.API_VERSION)[0])
) {
if (res.response) {
// 正常情況返回必有response 节点
// 返回结果的随机密钥
let masterKey;
if (!window.localStorage.getItem('masterKey')) {
return Promise.reject('密钥失效')
} else {
masterKey = window.localStorage.getItem('masterKey');
}
console.log('请求结果response' + response)
console.log('请求结果masterKey' + masterKey)
console.log(JSON.parse(AESDecrypt(response.data.response, masterKey)))
// 使用随机密钥解密返回结果data
res = JSON.parse(AESDecrypt(res.response, masterKey))
// res = JSON.parse(AESTools.AESDecrypt(res.response, configApp.REQ_PWD))
if(configApp.API_VERSION == 'v3'){
// 正常情況返回必有response 节点
// 返回结果的随机密钥
let masterKey;
if (!window.localStorage.getItem('masterKey')) {
return Promise.reject('密钥失效')
} else {
masterKey = window.localStorage.getItem('masterKey');
}
console.log('请求结果response' + response)
console.log('请求结果masterKey' + masterKey)
console.log(JSON.parse(AESDecrypt(response.data.response, masterKey)))
// 使用随机密钥解密返回结果data
res = JSON.parse(AESDecrypt(res.response, masterKey))
}else if(configApp.API_VERSION == 'v2'){
// 使用固定密钥解密返回结果data
res = JSON.parse(AESDecrypt(res.response, configApp.REQ_PWD))
}
}
}