13404089107
2025-05-08 b7ec20b3ec22c858f2db3d9285c5e9d38bd8a48f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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