javascript64位加密

var base64 = new Object();
        base64.settings = {
            char: "+/",
            pad: "=",
            ascii: false
        };
        base64.char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + base64.settings.char;
        base64.encode = function (g) {
            var a = "";
            var b = "";
            for (var d = 0; d < g.length; ++d) {
                var c = g.charCodeAt(d);
                if (this.settings.ascii) {
                    if (c >= 256) {
                        throw "Not an 8-bit char."
                    }
                }
                var e = c.toString(2);
                while (e.length < (this.settings.ascii ? 8 : 16)) {
                    e = "0" + e
                }
                b += e;
                while (b.length >= 6) {
                    var f = b.slice(0, 6);
                    b = b.slice(6);
                    a += base64.char_set.charAt(parseInt(f, 2))
                }
            }
            if (b) {
                while (b.length < 6) {
                    b += "0"
                }
                a += base64.char_set.charAt(parseInt(b, 2))
            }
            if (this.settings.pad) {
                while (a.length % (this.settings.ascii ? 4 : 8) != 0) {
                    a += this.settings.pad
                }
            }
            return a
        };
        base64.decode = function (j) {
            var c = "";
            var b = "";
            var k = (this.settings.ascii ? 8 : 16);
            for (var f = 0; f < j.length; ++f) {
                if (j[f] == this.settings.pad) {
                    break
                }
                var a = base64.char_set.indexOf(j.charAt(f));
                var h = a.toString(2);
                while (h.length < 6) {
                    h = "0" + h
                }
                b += h;
                while (b.length >= k) {
                    var g = b.slice(0, k);
                    b = b.slice(k);
                    c += String.fromCharCode(parseInt(g, 2))
                }
            }
            var d = c.split("");
            var e = "";
            for (f = 0; f < d.length; f++) {
                if (d[f].charCodeAt(0) > 20) {
                    e += d[f]
                }
            }
            return e
        };

        
        var str = base64.encode("杨秀徐");
        alert("base64 encode:" + str);
     
        str = base64.decode(str);
        alert("base64 decode:" + str); 
原文地址:https://www.cnblogs.com/sntetwt/p/3387814.html