From 22d2c5dba114a2bb0fe7dc2b01ff33a4d1e9a2f4 Mon Sep 17 00:00:00 2001 From: jiang Date: Wed, 6 Nov 2024 10:05:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/config.js | 1 + src/utils/configure.js | 16 +++++++++++++++- src/utils/request.js | 15 +++++---------- src/utils/sm.js | 31 +++++++++++++++++++++---------- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/utils/config.js b/src/utils/config.js index 27d3fe8f..9c1f849e 100644 --- a/src/utils/config.js +++ b/src/utils/config.js @@ -2,6 +2,7 @@ import { getConfig } from '@/api/config'; export function get() { getConfig() .then(response => { + console.log(response) localStorage.setItem('systemConfig', JSON.stringify(response.data)); }) .catch(error => { diff --git a/src/utils/configure.js b/src/utils/configure.js index 4566f0c4..f3e168eb 100644 --- a/src/utils/configure.js +++ b/src/utils/configure.js @@ -1,7 +1,8 @@ // SM 配置 const SM_CONFIG = { SALT: '2cc0c5f9f1749f1632efa9f63e902323', // SM3 盐值(16 字节) - SM4_KEY: 'your-sm4-key', // SM4 对称加密密钥 + SM4_KEY:"78d1295afa99449b99d6f83820e6965c", // SM4 对称加密密钥 + SM4_SALT:generateUUID(), SM2_PUBLIC_KEY: 'your-public-key', // SM2 公钥 SM2_PRIVATE_KEY: 'your-private-key' // SM2 私钥 } @@ -10,6 +11,19 @@ const AES_CONFIG = { AES_KEY: 'zhgd@bonus@zhgd@bonus@1234567890', // AES key值 AES_IV: '1234567812345678' // AES 偏移量 } + +export function generateUUID() { + // 使用当前时间戳和随机数生成一个 UUID + return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + const r = Math.random() * 16 | 0; // 生成随机数 + const v = c === 'x' ? r : (r & 0x3 | 0x8); // 根据 UUID 规范生成相应的值 + return v.toString(16); // 转换为十六进制 + }); +} + +// 使用示例 +const uuid = generateUUID(); +console.log(uuid); module.exports = { SM_CONFIG, AES_CONFIG, diff --git a/src/utils/request.js b/src/utils/request.js index ef0cc7d5..eef9240a 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -7,9 +7,9 @@ import { tansParams, blobValidate } from '@/utils/bonus' import cache from '@/plugins/cache' import { saveAs } from 'file-saver' import { encryptCBC, decryptCBC } from '@/utils/aescbc' -import { hashWithSM3AndSalt } from '@/utils/sm' -const systemConfig = JSON.parse(localStorage.getItem('systemConfig')) || { - requestConfig: { encryptRequest: false, checkIntegrity: false, encryptResponse: false } +import { decryptWithSM4, encryptWithSM4, hashWithSM3AndSalt } from '@/utils/sm' +const systemConfig = { + requestConfig: { encryptRequest: true, checkIntegrity: true, encryptResponse: true } }; let downloadLoadingInstance @@ -64,14 +64,9 @@ service.interceptors.request.use(config => { let data = typeof config.data === 'object' ? JSON.stringify(config.data) : config.data let contentType = config.headers['Content-Type'] if (contentType.includes('application/json') && typeof data !== 'undefined') { - // 数据完整性校验 - if (systemConfig.requestConfig.checkIntegrity && checkIntegrity) { - config.headers['Params-Hash'] = hashWithSM3AndSalt(data) - config.data = data - } // 加密数据 if (systemConfig.requestConfig.encryptRequest && encryptRequest) { - config.data = encryptCBC(data) + config.data = encryptWithSM4(data+"|"+hashWithSM3AndSalt(data)) } } // 检查请求数据大小 @@ -101,7 +96,7 @@ service.interceptors.request.use(config => { // 响应拦截器 service.interceptors.response.use(res => { if (res.headers.encryptresponse && !res.data.hasOwnProperty('code')) { - res.data = JSON.parse(decryptCBC(res.data)) + res.data = JSON.parse(decryptWithSM4(res.data)) } // 未设置状态码则默认成功状态 const code = res.data.code || 200 diff --git a/src/utils/sm.js b/src/utils/sm.js index a3696bff..9d8c15c4 100644 --- a/src/utils/sm.js +++ b/src/utils/sm.js @@ -2,7 +2,8 @@ import { sm2, sm3, sm4 } from 'sm-crypto' // 配置项,例如盐值、SM2 公私钥、SM4 密钥 import { SM_CONFIG } from './configure' - +import SM4 from 'sm-crypto/src/sm4' +import { hexToArray } from 'sm-crypto/src/sm2/utils' // SM3 哈希 export function hashSM3(text) { @@ -29,15 +30,25 @@ export function decryptWithSM2(encryptedText) { // SM2 私钥解密 return sm2.doDecrypt(encryptedText, SM_CONFIG.SM2_PRIVATE_KEY) } - -// SM4 加密 -export function encryptWithSM4(text) { - // SM4 对称加密,ECB 模式 - return sm4.encrypt(text, SM_CONFIG.SM4_KEY) +/** + * 加密函数 + * @param {string} plainText + * @returns {string} 加密后的密文(Hex 编码格式) + */ +export function encryptWithSM4(plainText) { + const salt =SM_CONFIG.SM4_SALT + return sm4.encrypt(plainText, SM_CONFIG.SM4_KEY,{ mode: 'cbc', padding: 'pkcs#5',iv:salt})+salt; } -// SM4 解密 -export function decryptWithSM4(encryptedText) { - // SM4 对称解密,ECB 模式 - return sm4.decrypt(encryptedText, SM_CONFIG.SM4_KEY) +/** + * 解密函数 + * @param {string} cipherText + * @returns {string} 解密后的明文 + */ +export function decryptWithSM4(cipherText){ + const length = cipherText.length; + const salt = length > 32 ? cipherText.substring(length - 32) : cipherText; + const originalHex = length > 32 ? cipherText.substring(0, length - 32) : ''; + return SM4.decrypt(originalHex, SM_CONFIG.SM4_KEY,{ mode: 'cbc', padding: 'pkcs#5' ,iv:salt}); } +