【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)
相关推荐
影寂ldy3 分钟前
C#数组的属性和方法(Clear / Copy / IndexOf )
开发语言·javascript·c#
Brave & Real17 分钟前
小程序 const 在js中以及与同类的var和let之间的差异
javascript·微信小程序·小程序
米丘1 小时前
React 19.x 的 lazy 与 Suspense
前端·javascript·react.js
padane222 小时前
gmssl编译wasm
ubuntu·html·密码学·wasm·js
ZC跨境爬虫2 小时前
跟着 MDN 学CSS day_21:(图像溢出控制与表单元素样式定制)
前端·javascript·css·ui·交互
riuphan2 小时前
JavaScript 中的 this 关键字
javascript
掰头战士2 小时前
五分钟带你深入了解 this
javascript
biubiubiu_LYQ2 小时前
萌新小白基础理解篇之 this 关键字
前端·javascript
甜味弥漫2 小时前
深度解析 JS 中的 this 指向:从底层逻辑到实战规则
javascript·面试
爱上好庆祝3 小时前
学习JS第十一天(JS的进阶)
前端·javascript·学习