AI-accounting-soft-uniApp/utils/crypto.js

57 lines
1.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 简单的密码加密/解密工具Base64编码 + 简单混淆)
// 注意:这不是强加密,只是防止密码明文存储
const SECRET_KEY = 'QIANGBAO_AI_JIZHANG_2024' // 密钥
/**
* 加密密码
* @param {string} password - 原始密码
* @returns {string} - 加密后的密码
*/
export function encryptPassword(password) {
if (!password) return ''
try {
// 1. 将密码转为字符数组
const chars = password.split('')
// 2. 与密钥进行简单的XOR混淆
const encrypted = chars.map((char, index) => {
const keyChar = SECRET_KEY[index % SECRET_KEY.length]
return String.fromCharCode(char.charCodeAt(0) ^ keyChar.charCodeAt(0))
}).join('')
// 3. Base64编码
return btoa(unescape(encodeURIComponent(encrypted)))
} catch (error) {
console.error('密码加密失败:', error)
return password // 失败时返回原密码
}
}
/**
* 解密密码
* @param {string} encryptedPassword - 加密的密码
* @returns {string} - 解密后的密码
*/
export function decryptPassword(encryptedPassword) {
if (!encryptedPassword) return ''
try {
// 1. Base64解码
const decoded = decodeURIComponent(escape(atob(encryptedPassword)))
// 2. 与密钥进行XOR反混淆
const chars = decoded.split('')
const decrypted = chars.map((char, index) => {
const keyChar = SECRET_KEY[index % SECRET_KEY.length]
return String.fromCharCode(char.charCodeAt(0) ^ keyChar.charCodeAt(0))
}).join('')
return decrypted
} catch (error) {
console.error('密码解密失败:', error)
return '' // 失败时返回空字符串
}
}