From 68f09fcb7d00d3a58ee827f8457328c29e489df7 Mon Sep 17 00:00:00 2001 From: jiang Date: Fri, 8 Nov 2024 15:01:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/configure.js | 9 +-------- src/utils/request.js | 3 ++- src/utils/sm.js | 8 ++------ 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/utils/configure.js b/src/utils/configure.js index 1c966d84..6e26ee8a 100644 --- a/src/utils/configure.js +++ b/src/utils/configure.js @@ -2,7 +2,7 @@ export const SM_CONFIG = { SALT: '2cc0c5f9f1749f1632efa9f63e902323', // SM3 盐值(16 字节) SM4_KEY:"78d1295afa99449b99d6f83820e6965c", // SM4 对称加密密钥 - SM4_SALT:generateUUID(), + SM4_SALT:"f555adf6c01d0ab0761e626a2dae34a2", SM2_PUBLIC_KEY: 'your-public-key', // SM2 公钥 SM2_PRIVATE_KEY: 'your-private-key' // SM2 私钥 } @@ -21,10 +21,3 @@ export function generateUUID() { }); } -// // 使用示例 -// 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 d7ba1333..7d2be68e 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -6,7 +6,6 @@ import errorCode from '@/utils/errorCode' import { tansParams, blobValidate } from '@/utils/bonus' import cache from '@/plugins/cache' import { saveAs } from 'file-saver' -import { encryptCBC, decryptCBC } from '@/utils/aescbc' import { decryptWithSM4, encryptWithSM4, hashWithSM3AndSalt } from '@/utils/sm' const systemConfig = JSON.parse(localStorage.getItem('systemConfig')) || { requestConfig: { encryptRequest: false, checkIntegrity: false, encryptResponse: false } @@ -66,6 +65,8 @@ service.interceptors.request.use(config => { if (contentType.includes('application/json') && typeof data !== 'undefined') { // 加密数据 if (systemConfig.requestConfig.encryptRequest && encryptRequest) { + console.log(data); + console.log(hashWithSM3AndSalt(data)); config.data = encryptWithSM4(data+"|"+hashWithSM3AndSalt(data)) } } diff --git a/src/utils/sm.js b/src/utils/sm.js index 9d8c15c4..8ed7008b 100644 --- a/src/utils/sm.js +++ b/src/utils/sm.js @@ -36,8 +36,7 @@ export function decryptWithSM2(encryptedText) { * @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; + return sm4.encrypt(plainText, SM_CONFIG.SM4_KEY,{ mode: 'cbc', padding: 'pkcs#5',iv:SM_CONFIG.SM4_SALT}); } /** @@ -46,9 +45,6 @@ export function encryptWithSM4(plainText) { * @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}); + return SM4.decrypt(cipherText, SM_CONFIG.SM4_KEY,{ mode: 'cbc', padding: 'pkcs#5' ,iv:SM_CONFIG.SM4_SALT}); }