JavaScript Base64加解密

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript Base64加解密相关的知识,希望对你有一定的参考价值。

Base64加密算法是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的详细规范。Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到。

下面是Base64加密、解密算法的简单实现(不支持中文):

function base64Encrypt(baseString) {
    var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var length = baseString.length;
    var loopNo = parseInt(length / 3);
    var charOne, charTwo, charThree;
    var out = "";
    for (var i = 0, j = 0; j < loopNo; j++) {
        charOne = baseString.charCodeAt(i++);
        charTwo = baseString.charCodeAt(i++);
        charThree = baseString.charCodeAt(i++);
        out += base64Chars.charAt(charOne >> 2);
        out += base64Chars.charAt(((charOne & 0x3) << 4) | ((charTwo & 0xF0) >> 4));
        out += base64Chars.charAt(((charTwo & 0xF) << 2) | ((charThree & 0xC0) >> 6));
        out += base64Chars.charAt(charThree & 0x3F);
    }
    if (i === length) {
        return out;
    }
    charOne = baseString.charCodeAt(i++) & 0xff;
    out += base64Chars.charAt(charOne >> 2);
    if (i === length) {
        out += base64Chars.charAt((charOne & 0x3) << 4);
        out += "==";
    } else {
        charTwo = baseString.charCodeAt(i++);
        out += base64Chars.charAt(((charOne & 0x3) << 4) | ((charTwo & 0xF0) >> 4));
        out += base64Chars.charAt((charTwo & 0xF) << 2);
        out += "=";
    }
    return out;
}

function base64Encode(baseSting) {
    var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var length = baseSting.length;
    var chara1, chara2, chara3;
    var out = "", i = 0;
    while (i < length) {
        chara1 = baseSting.charCodeAt(i++) & 0xff;
        out += base64Chars.charAt(chara1 >> 2);
        if (i == length) {
            out += base64Chars.charAt((chara1 & 0x3) << 4);
            out += "==";
            break;
        }
        chara2 = baseSting.charCodeAt(i++);
        if (i == length) {
            out += base64Chars.charAt(((chara1 & 0x3) << 4) | ((chara2 & 0xF0) >> 4));
            out += base64Chars.charAt((chara2 & 0xF) << 2);
            out += "=";
            break;
        }
        chara3 = baseSting.charCodeAt(i++);
        out += base64Chars.charAt(((chara1 & 0x3) << 4) | ((chara2 & 0xF0) >> 4));
        out += base64Chars.charAt(((chara2 & 0xF) << 2) | ((chara3 & 0xC0) >> 6));
        out += base64Chars.charAt(chara3 & 0x3F);
    }
    return out;
}

function base64Decode(baseString) {
    var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var length = baseString.length;
    var charI, charII, charIII, charIV;
    var out = "", i = 0;
    if (0 < length && 0 === length % 4) {
        if (/^[A-Za-z0-9\+/]{2,}={0,2}$/.test(baseString)) {
            while (i < length - 4) {
                charI = base64Chars.indexOf(baseString.charAt(i++));
                charII = base64Chars.indexOf(baseString.charAt(i++));
                charIII = base64Chars.indexOf(baseString.charAt(i++));
                charIV = base64Chars.indexOf(baseString.charAt(i++));
                out += String.fromCharCode((charI << 2) | ((charII & 0x30) >> 4));
                out += String.fromCharCode(((charII & 0xF) << 4) | ((charIII & 0x3C) >> 2));
                out += String.fromCharCode(((charIII & 0x3) << 6) | charIV);
            }
            charI = base64Chars.indexOf(baseString.charAt(i++));
            charII = base64Chars.indexOf(baseString.charAt(i++));
            out += String.fromCharCode((charI << 2) | ((charII & 0x30) >> 4));

            switch (baseString.indexOf("=")) {
                case length - 1:
                    charIII = base64Chars.indexOf(baseString.charAt(i++));
                    out += String.fromCharCode(((charII & 0xF) << 4) | ((charIII & 0x3C) >> 2));
                    break;
                case -1:
                    charIII = base64Chars.indexOf(baseString.charAt(i++));
                    charIV = base64Chars.indexOf(baseString.charAt(i++));
                    out += String.fromCharCode(((charII & 0xF) << 4) | ((charIII & 0x3C) >> 2));
                    out += String.fromCharCode(((charIII & 0x3) << 6) | charIV);
            }
        } else {
            throw new Error(baseString + " does not base64 encoded string.");
        }
    } else {
        throw new Error(baseString + " does not base64 encoded string.");
    }
    return out;
}

 

以上是关于JavaScript Base64加解密的主要内容,如果未能解决你的问题,请参考以下文章

java之BASE64加解密

在线加解密工具

Python AES - base64 加解密

vue中使用base64进行加解密

在线加/解密

加解密算法之Base64