Files
api-box/frontend/src/utils/encryption.js
T
2025-10-05 18:12:51 +08:00

36 lines
971 B
JavaScript

// AES-256 加密/解密工具
// 注意:这是一个简化版本,实际生产环境应使用更安全的加密库
import CryptoJS from 'crypto-js'
// 密钥(在实际应用中应使用更安全的方式管理)
const SECRET_KEY = 'ai-api-tool-secret-key-2025'
/**
* 加密函数
* @param {string} text - 要加密的文本
* @returns {string} - 加密后的文本
*/
export function encrypt(text) {
try {
return CryptoJS.AES.encrypt(text, SECRET_KEY).toString()
} catch (error) {
console.error('加密失败:', error)
throw new Error('加密失败')
}
}
/**
* 解密函数
* @param {string} encryptedText - 要解密的文本
* @returns {string} - 解密后的文本
*/
export function decrypt(encryptedText) {
try {
const bytes = CryptoJS.AES.decrypt(encryptedText, SECRET_KEY)
return bytes.toString(CryptoJS.enc.Utf8)
} catch (error) {
console.error('解密失败:', error)
throw new Error('解密失败')
}
}