JS 避免明文,中文乱了等问题的简单XOR基础加密实现


设置key

static aeskey = "xiaoguan-qingdao-20251127";


加密

// 替代aesEncode的简单加密方法
static aesEncode(text) {
    if (!text) return '';
    let result = '';
    for (let i = 0; i < text.length; i++) {
        const charCode = text.charCodeAt(i) ^ this.aeskey.charCodeAt(i % this.aeskey.length);
        result += String.fromCharCode(charCode);
    }
    return btoa(unescape(encodeURIComponent(result)));
}

解密

// 替代aesDecode的简单解密方法
static aesDecode(encoded) {
    if (!encoded) return '';
    try {
        const text = decodeURIComponent(escape(atob(encoded)));
        let result = '';
        for (let i = 0; i < text.length; i++) {
            const charCode = text.charCodeAt(i) ^ this.aeskey.charCodeAt(i % this.aeskey.length);
            result += String.fromCharCode(charCode);
        }
        return result;
    } catch (e) {
        console.log('解密失败: ' + e.message);
        return '';
    }
}