mirror of
http://112.124.100.131/GFRS/ebiz-h5.git
synced 2025-12-15 10:36:44 +08:00
【前端开发】随机密钥加密解密--修改request.js和cryptoJsUtil.js脚本
This commit is contained in:
11
package-lock.json
generated
11
package-lock.json
generated
@@ -4050,9 +4050,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"crypto-js": {
|
"crypto-js": {
|
||||||
"version": "4.0.0",
|
"version": "4.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.1.1.tgz",
|
||||||
"integrity": "sha512-bzHZN8Pn+gS7DQA6n+iUmBfl0hO5DJq++QP3U6uTucDtk/0iGpXd/Gg7CGR0p8tJhofJyaKoWBuJI4eAO00BBg=="
|
"integrity": "sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw=="
|
||||||
},
|
},
|
||||||
"css": {
|
"css": {
|
||||||
"version": "2.2.4",
|
"version": "2.2.4",
|
||||||
@@ -9559,6 +9559,11 @@
|
|||||||
"xml-name-validator": "^3.0.0"
|
"xml-name-validator": "^3.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"jsencrypt": {
|
||||||
|
"version": "3.2.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/jsencrypt/-/jsencrypt-3.2.1.tgz",
|
||||||
|
"integrity": "sha512-k1sD5QV0KPn+D8uG9AdGzTQuamt82QZ3A3l6f7TRwMU6Oi2Vg0BsL+wZIQBONcraO1pc78ExMdvmBBJ8WhNYUA=="
|
||||||
|
},
|
||||||
"jsesc": {
|
"jsesc": {
|
||||||
"version": "2.5.2",
|
"version": "2.5.2",
|
||||||
"resolved": "https://registry.npmjs.org/jsesc/-/jsesc-2.5.2.tgz",
|
"resolved": "https://registry.npmjs.org/jsesc/-/jsesc-2.5.2.tgz",
|
||||||
|
|||||||
@@ -23,11 +23,12 @@
|
|||||||
"animate.css": "^4.1.1",
|
"animate.css": "^4.1.1",
|
||||||
"axios": "^0.19.0",
|
"axios": "^0.19.0",
|
||||||
"core-js": "^2.6.5",
|
"core-js": "^2.6.5",
|
||||||
"crypto-js": "^4.0.0",
|
"crypto-js": "^4.1.1",
|
||||||
"echarts": "^4.2.1",
|
"echarts": "^4.2.1",
|
||||||
"fastclick": "^1.0.6",
|
"fastclick": "^1.0.6",
|
||||||
"js-base64": "^2.5.1",
|
"js-base64": "^2.5.1",
|
||||||
"js-md5": "^0.7.3",
|
"js-md5": "^0.7.3",
|
||||||
|
"jsencrypt": "^3.2.1",
|
||||||
"nprogress": "^0.2.0",
|
"nprogress": "^0.2.0",
|
||||||
"pdfh5": "^1.2.13",
|
"pdfh5": "^1.2.13",
|
||||||
"vant": "2.2.0",
|
"vant": "2.2.0",
|
||||||
|
|||||||
@@ -1,5 +1,23 @@
|
|||||||
|
import { JSEncrypt } from 'jsencrypt'
|
||||||
import CryptoJS from 'crypto-js'
|
import CryptoJS from 'crypto-js'
|
||||||
export default {
|
export default {
|
||||||
|
// 生成本次请求随机密钥
|
||||||
|
randomString: function(len) {
|
||||||
|
len = len || 24
|
||||||
|
var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678oOLl9gqVvUuI1'
|
||||||
|
var maxPos = $chars.length
|
||||||
|
var pwd = ''
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
|
||||||
|
}
|
||||||
|
return pwd
|
||||||
|
},
|
||||||
|
// RSA加密,公钥加密
|
||||||
|
RSAEncrypt : function (data, publicKey) {
|
||||||
|
let crypt = new JSEncrypt()
|
||||||
|
crypt.setPublicKey(publicKey)
|
||||||
|
return crypt.encrypt(data);
|
||||||
|
},
|
||||||
AESEncrypt: function(data, keys) {
|
AESEncrypt: function(data, keys) {
|
||||||
//加密
|
//加密
|
||||||
var key = CryptoJS.enc.Hex.parse(keys)
|
var key = CryptoJS.enc.Hex.parse(keys)
|
||||||
@@ -45,3 +63,14 @@ export default {
|
|||||||
return CryptoJS.enc.Utf8.stringify(decrypted)
|
return CryptoJS.enc.Utf8.stringify(decrypted)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function format(base64, isPrivate = 0) {
|
||||||
|
var text = "-----BEGIN " + (isPrivate ? "PRIVATE" : "PUBLIC") + " KEY-----\n";
|
||||||
|
text += base64.replace(/[^\x00-\xff]/g, "$&\x01").replace(/.{64}\x01?/g, "$&\n");
|
||||||
|
text += "\n-----END " + (isPrivate ? "PRIVATE" : "PUBLIC") + " KEY-----";
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
export let MD5 = function (key) {
|
||||||
|
return CryptoJS.MD5(key);
|
||||||
|
}
|
||||||
|
export let publicKey = format(`MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApkixN3Dc6BLzb/V74VpxRXsSIu9AabGmK4xfcPiIqub0JS99a+P6XAOGuiMT2W4p1C8U9MZDRgHjUOrKGcc5ve9uT+U90LiAgwG58YdrklOTwlGvo6Xh4HQLRXMNoGsn6jLGdOV1RIVfWQ5EWfEB1+5v86QarLyfLIJ4ujVQfafEJ4dCwmoNSJk8xqVBAW9tDZlNOOgaZPJuEXVIFEEjIZCkFkFxkomwVNdp79Xewrj0mCybCDVy6Mcx3jOxY0gGwbGgS3YQxDbOpqYna8rcmf6CVJ2GA75sCU61Y8Of244CR5Rwkspbr1Pbf4UNSbVbpxzI08z1jrJvCVYWNQLMwwIDAQAB`);
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import AESTools from '@/assets/js/utils/cryptoJsUtil'
|
// import AESTools from '@/assets/js/utils/cryptoJsUtil'
|
||||||
|
import {
|
||||||
|
MD5,
|
||||||
|
RSAEncrypt,
|
||||||
|
AESEncrypt,
|
||||||
|
AESDecrypt,
|
||||||
|
randomString,
|
||||||
|
publicKey,
|
||||||
|
} from '@/assets/js/utils/cryptoJsUtil'
|
||||||
import configApp from '@/config'
|
import configApp from '@/config'
|
||||||
import { Dialog, Toast } from 'vant'
|
import { Dialog, Toast } from 'vant'
|
||||||
import MD5 from 'js-md5'
|
// import MD5 from 'js-md5'
|
||||||
import CacheUtils from '@/assets/js/utils/cacheUtils'
|
import CacheUtils from '@/assets/js/utils/cacheUtils'
|
||||||
import BusinessCommon from '@/assets/js/business-common'
|
import BusinessCommon from '@/assets/js/business-common'
|
||||||
|
|
||||||
@@ -147,16 +155,42 @@ service.interceptors.request.use(
|
|||||||
message: '加载中……'
|
message: '加载中……'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
// 生成本次请求随机密钥
|
||||||
|
let masterKey
|
||||||
|
if (!window.localStorage.getItem('masterKey')) {
|
||||||
|
masterKey = randomString();
|
||||||
|
window.localStorage.setItem('masterKey',masterKey)
|
||||||
|
} else {
|
||||||
|
masterKey = window.localStorage.getItem('masterKey')
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 请求拦截处理(待添加 判断走统一网关处理)
|
* 请求拦截处理(待添加 判断走统一网关处理)
|
||||||
*/
|
*/
|
||||||
|
console.log(config)
|
||||||
if (config.url && /api\/$/.test(config.url.split(configApp.API_VERSION)[0]) && configApp.API_VERSION == 'v2') {
|
if (config.url && /api\/$/.test(config.url.split(configApp.API_VERSION)[0]) && configApp.API_VERSION == 'v2') {
|
||||||
if (!config.data || config.data == null) {
|
if (!config.data || config.data == null) {
|
||||||
config.data = {}
|
config.data = {}
|
||||||
}
|
}
|
||||||
if (!!config.data && config.data != null) {
|
if (!!config.data && config.data != null) {
|
||||||
let encrypt = AESTools.AESEncrypt(JSON.stringify(config.data), configApp.REQ_PWD)
|
// let encrypt = AESTools.AESEncrypt(JSON.stringify(config.data), configApp.REQ_PWD)
|
||||||
config.data = { data: encrypt }
|
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));
|
||||||
|
}
|
||||||
|
// config.data = { data: encrypt }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,7 +242,19 @@ service.interceptors.response.use(
|
|||||||
) {
|
) {
|
||||||
if (res.response) {
|
if (res.response) {
|
||||||
// 正常情況返回必有response 节点
|
// 正常情況返回必有response 节点
|
||||||
res = JSON.parse(AESTools.AESDecrypt(res.response, configApp.REQ_PWD))
|
// 返回结果的随机密钥
|
||||||
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user