11.base64

作者: 原来哥哥是万家灯火 | 来源:发表于2020-07-03 12:57 被阅读0次

    base64,是用64个可打印字符来表示二进制数据的编码方式。这64个字符分别用A ~ Z,a ~ z,0 ~ 9,+,/
    之所以使用base64,是因为文本传输协议传输二进制时,不同网络设备对不可打印字符的处理不一样,所以转化为用可打印字符表示。

    原理:一串二进制数据,以每六位为一个基本单元,对应一个可打印字符。
    3个字节的原数据,转化成base64后是4个字节,有 1/3 的冗余。

    其编码过程如下:
    1.将二进制数据,在末尾加 0 ,补成 3 字节的整数倍。
    2.每 6 个位一组,计算出其十进制下的对应值,用对应的字符代替。
    3.补了几个字节的0,最后几个单元忽略第2步,直接用 = 代替。

    例1. 将ASCII中 A 的二进制编码 01000001转为 base64
    1.补充两个字节的0
    01000001 00000000 00000000

    2.每6位分组
    010000 010000 000000 000000

    3.计算出每个单元对应的十进制(补了几个字节的0,最后几个单元忽略第2步,直接用 “=” 代替)
    16 16 = =

    4.根据十进制数转化成对应的可打印字符
    arr = [A ~ Z,a ~ z,0 ~ 9,+,/] 中,arr[16] 是Q,所以:
    最终结果,ASCII中A的base64编码是QQ==

    例2. 将ASCII中 BC 的二进制编码 01000010 01000011转为 base64
    1.补充一个字节的0
    01000010 01000011 00000000

    2.每6位分组
    010000 100100 001100 000000

    3.计算出每个单元对应的十进制(6 位全为补加的 0 的单元忽略不管,直接用 “=” 代替)
    16 36 12 =

    4.根据十进制数转化成对应的可打印字符
    arr = [A ~ Z,a ~ z,0 ~ 9,+,/] 中,arr[16] 是Q,arr[36]是 k ,arr[12]是 M。所以:
    最终结果,ASCII中BC的base64编码是QkM=

    javascript实现base64编码算法

    function base64Encode(binaryString) {
        let byte = binaryString.length / 8;
        let addedByte = (byte % 3 === 0) ? 0 : (byte % 3 === 1) ? 2 : 1;
        let suffix = '0'.repeat(addedByte * 8);
        binaryString = binaryString + suffix;
    
        let base64 = [
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
            'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
            'w', 'x', 'y', 'z', '0', '1', '2', '3',
            '4', '5', '6', '7', '8', '9', '+', '/'
        ];
    
        let result = '';
        for (let i = 0, len = binaryString.length / 6; i < len; i++) {
            if (i < len - addedByte) {
                let unit = binaryString.slice(6 * i, 6 * i + 6);
                let index = parseInt(unit, 2);
                let character = base64[index];
                result += character;
            }
        }
    
        // 补了多少字节的“0”,最后就有几个单元就不需要转化,直接使用"="代替
        let suffix2 = '='.repeat(addedByte);
        result += suffix2;
    
        return result;
    }
    

    javascript实现base64解码算法

    function base64Decode(base64String) {
        let base64 = {
            "A": "000000", "B": "000001", "C": "000010", "D": "000011",
            "E": "000100", "F": "000101", "G": "000110", "H": "000111",
            "I": "001000", "J": "001001", "K": "001010", "L": "001011",
            "M": "001100", "N": "001101", "O": "001110", "P": "001111",
            "Q": "010000", "R": "010001", "S": "010010", "T": "010011",
            "U": "010100", "V": "010101", "W": "010110", "X": "010111",
            "Y": "011000", "Z": "011001", "a": "011010", "b": "011011",
            "c": "011100", "d": "011101", "e": "011110", "f": "011111",
            "g": "100000", "h": "100001", "i": "100010", "j": "100011",
            "k": "100100", "l": "100101", "m": "100110", "n": "100111",
            "o": "101000", "p": "101001", "q": "101010", "r": "101011",
            "s": "101100", "t": "101101", "u": "101110", "v": "101111",
            "w": "110000", "x": "110001", "y": "110010", "z": "110011",
            "0": "110100", "1": "110101", "2": "110110", "3": "110111",
            "4": "111000", "5": "111001", "6": "111010", "7": "111011",
            "8": "111100", "9": "111101", "+": "111110", "/": "111111",
            "=": "000000"
        }
        let result = '';
        for (let index in base64String) {
            let character = base64String[index];
            result += base64[character];
        }
        
        // 末尾有几个"=",就减去几个字节的"0"
        let len = base64String.endsWith('==') ? 2 : base64String.endsWith('=') ? 1 : 0;
        result = result.slice(0, result.length - len * 8);
    
        return result;
    }
    

    除了base64以外,还有base16(hex)、base32

    相关文章

      网友评论

          本文标题:11.base64

          本文链接:https://www.haomeiwen.com/subject/jjksqktx.html