【base64加密】js/ts的基础加密

base64的字符串简单加密,主用于网页缓存数据的加密。

适用于常规html、小游戏(egret、cocos、laya)等

原文参考:JS基于base64编码加密解密文本和图片(修订)_js base64加密-CSDN博客

测试:JS实现base64加密解密的方法 - web开发 - 亿速云

javascript 复制代码
export class Base64 {

    private static _keyStr: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    /**字符串加密*/
    public static encode(e) {
        var t = "";
        var n, r, i, s, o, u, a;
        var f = 0;
        e = Base64._utf8_encode(e);
        while (f < e.length) {
            n = e.charCodeAt(f++);
            r = e.charCodeAt(f++);
            i = e.charCodeAt(f++);
            s = n >> 2;
            o = (n & 3) << 4 | r >> 4;
            u = (r & 15) << 2 | i >> 6;
            a = i & 63;
            if (isNaN(r)) {
                u = a = 64
            } else if (isNaN(i)) {
                a = 64
            }
            t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
        }
        return t
    }

    /**字符串解密*/
    public static decode(e) {
        var t = "";
        var n, r, i;
        var s, o, u, a;
        var f = 0;
        e = e.replace(/[^A-Za-z0-9+/=]/g, "");
        while (f < e.length) {
            s = this._keyStr.indexOf(e.charAt(f++));
            o = this._keyStr.indexOf(e.charAt(f++));
            u = this._keyStr.indexOf(e.charAt(f++));
            a = this._keyStr.indexOf(e.charAt(f++));
            n = s << 2 | o >> 4;
            r = (o & 15) << 4 | u >> 2;
            i = (u & 3) << 6 | a;
            t = t + String.fromCharCode(n);
            if (u != 64) {
                t = t + String.fromCharCode(r)
            }
            if (a != 64) {
                t = t + String.fromCharCode(i)
            }
        }
        t = Base64._utf8_decode(t);
        return t
    }


    private static _utf8_encode(e) {
        e = e.replace(/rn/g, "n");
        var t = "";
        for (var n = 0; n < e.length; n++) {
            var r = e.charCodeAt(n);
            if (r < 128) {
                t += String.fromCharCode(r)
            } else if (r > 127 && r < 2048) {
                t += String.fromCharCode(r >> 6 | 192);
                t += String.fromCharCode(r & 63 | 128)
            } else {
                t += String.fromCharCode(r >> 12 | 224);
                t += String.fromCharCode(r >> 6 & 63 | 128);
                t += String.fromCharCode(r & 63 | 128)
            }
        }
        return t
    }

    private static _utf8_decode(e) {
        let t = "";
        let n = 0;
        let r = 0;
        let c1 = 0;
        let c2 = 0;
        let c3 = 0;
        while (n < e.length) {
            r = e.charCodeAt(n);
            if (r < 128) {
                t += String.fromCharCode(r);
                n++
            } else if (r > 191 && r < 224) {
                c2 = e.charCodeAt(n + 1);
                t += String.fromCharCode((r & 31) << 6 | c2 & 63);
                n += 2
            } else {
                c2 = e.charCodeAt(n + 1);
                c3 = e.charCodeAt(n + 2);
                t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
                n += 3
            }
        }
        return t
    }
}

使用方法:

javascript 复制代码
    
                    const num = Base64.encode(this.editBox.string);
                    Log.log("jia mi:", num)


                    const num = Base64.decode(this.editBox.string);
                    Log.log("解密:", num)
相关推荐
polarisya18 小时前
vue搭建知识
前端·javascript·vue.js
map_vis_3d19 小时前
百度地图控件一览:缩放、比例尺、定位、城市列表、地图类型和 3D 导航
前端·javascript·地图·百度地图
Hilaku19 小时前
国内前端面试最离谱的现象:笔试题考手写 Promise.all
前端·javascript·程序员
hotflyff19 小时前
把 Tesseract 塞进单文件 HTML:浏览器离线 OCR 的完整技术方案
javascript·开源
GuWenyue19 小时前
我 push 了三个元素,React 说数组没变:把「不可变更新」的 6 个反例全写了一遍
前端·javascript·react.js
AI前端工程狮19 小时前
可缓存的前缀占 92.8%,一个时间戳只剩 12.3%:Agent 缓存为什么总不命中
javascript·人工智能·程序员
wei_shuo19 小时前
SQL Server数据迁移实战:QueryMapping智能调优与主动式性能优化
javascript·后端
时间的情敌20 小时前
Three.js 水位模拟、水体 Shader、罐体液位动态变化(水箱、水池、储罐液位)
开发语言·javascript·three.js
程序员蜡笔熊21 小时前
HTML attribute 和 DOM property,根本不是一回事
java·前端·html
pan_junbiao21 小时前
Go 语言变量的声明与初始化
开发语言·javascript·golang