import axios from 'axios' import apiConfig from './baseurl' let encryptionKey = '2022lab02ora12to' // 默认密钥 // 从接口获取密钥 export const fetchEncryptionKey = async () => { try { const response = await axios.get(`${apiConfig.baseURL}/api/system/getEncryptionKey`) if (response.data && response.data.code === 200) { // 转换为Buffer并验证字节长度 const keyBuffer = Buffer.from(response.data.data, 'utf-8') if (keyBuffer.length !== 16) { console.warn('无效密钥长度,使用默认密钥') return encryptionKey // 保持原有密钥 } // 存储原始字符串和Buffer两种格式 encryptionKey = response.data.data return encryptionKey } } catch (error) { console.error('获取加密密钥失败:', error) } } // 新增方法获取Buffer格式的密钥 export const getEncryptionKeyBuffer = () => { return Buffer.from(encryptionKey, 'utf-8') } // 获取当前密钥(保持字符串格式) export const getEncryptionKey = () => encryptionKey