TS异或加密并输出Base64字符串

在做游戏或应用的时候,一些本地数据为了安全需要加密。最简单的加密方式当然是异或加密了,但异或之后输出的是数据块,并非字符串,有效且省空间的做法是装数据块转成Base64了,Base64之后的空间占用约为原数据的133%左右.

字符串与数据块转换

因为在使用过种中,会用到数据块,这里用字节数组Uint8Array来存储数据块,并需要Uint8Array与String UTF8的互转.

    // 字符串转uint字节数组
    // 由于字符串中可能包含utf8不定长度字节的编码
    private str2bytes(str: string):Array<any> {
        let bytes = new Array();
        let len, c;
        len = str.length;
        for (let i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if (c >= 0x010000 && c <= 0x10FFFF) {
                bytes.push(((c >> 18) & 0x07) | 0xF0);
                bytes.push(((c >> 12) & 0x3F) | 0x80);
                bytes.push(((c >> 6) & 0x3F) | 0x80);
                bytes.push((c & 0x3F) | 0x80);
            } else if (c >= 0x000800 && c <= 0x00FFFF) {
                bytes.push(((c >> 12) & 0x0F) | 0xE0);
                bytes.push(((c >> 6) & 0x3F) | 0x80);
                bytes.push((c & 0x3F) | 0x80);
            } else if (c >= 0x000080 && c <= 0x0007FF) {
                bytes.push(((c >> 6) & 0x1F) | 0xC0);
                bytes.push((c & 0x3F) | 0x80);
            } else {
                bytes.push(c & 0xFF);
            }
        }
        return bytes;
    }

    // uint8字节数组转string utf8
    private bytes2str(uintArray: any): string {
        let encodedString = String.fromCharCode.apply(null, uintArray),
            decodedString = decodeURIComponent(escape(encodedString));
        return decodedString;
    }
    // 测试
    let base = "guoke3915";
    var tmp = this.str2bytes(base);
    console.log(tmp);   // 输出: 103,117,111,107,101,51,57,49,53

    var out = this.bytes2str(tmp);
    console.log(out )     // 输出: guoke3915

异或加密

网上也有很多异或的算法,很多都是用charCodeAtfromCharCode来对字符串直接操作的,这样的操作在ts中很不错也不会出问题.但我还是比较喜欢转成用字节数组来操作,在种在网络通讯中,跨平台跨语言时不容易出错.

    /**
      * encrypto 加密程序
      * @param {strng} str 待加密字符串
      * @param {number} xor 异或值
      * @return {Uint8Array} 加密后的字符串
    */
    private xor_encrypto(str: string, xor: number): Uint8Array {
        let buf = this.str2bytes(str)
        let bufView = new Uint8Array(buf.length);
        for (let i = 0; i < buf.length; i++) {
            // 进行异或加密
            bufView[i] = buf[i] ^ xor;
        }
        return bufView;
    }

    /**
     * decrypto 解密程序
     * @param {strng} str 待加密字符串
     * @param {number} xor 异或值
     * @return {strng} 解密后的字符串
     */
    private xor_decrypto(data: any, xor: number): string {
        let bytes = new Uint8Array(data)
        let resultList = new Uint8Array(bytes.length);
        // 分割出加密字符串的加密后的每个字符
        for (let i = 0; i < bytes.length; i++) {
            // 异或解密出原字符的ascll码
            resultList[i] = bytes[i] ^ xor;
        }
        return this.bytes2str(resultList);
    }
    // 测试
    let base = "guoke3915";
    var tmp = this.xor_encrypto(base);
    console.log(tmp);   // 输出: 80,66,88,92,82,4,14,6,2

    var out = this.xor_decrypto(tmp);
    console.log(out )     // 输出: guoke3915

Base64算法

Base64算法在js原生平台有库,使用Buffer类就能实现. Base64的算法也挺简单的,这里为了配合异或加密还是自己写一个.

    // base64 编码
    private base64_encode(input:string):string{
        const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        let data = this.str2bytes(input);

        while (i < data.length)
        {
            chr1 = data[i++];
            chr2 = data[i++];
            chr3 = data[i++];

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2))
            {
                enc3 = enc4 = 64;
            }
            else if (isNaN(chr3))
            {
                enc4 = 64;
            }

            output = output +
                _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
                _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
        }

        return output;
    }

    // base64 解码
    private base64_decode(input:string):string{
        const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        var output = new Uint8Array(input.length);
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0, index = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < input.length)
        {
            enc1 = _keyStr.indexOf(input.charAt(i++));
            enc2 = _keyStr.indexOf(input.charAt(i++));
            enc3 = _keyStr.indexOf(input.charAt(i++));
            enc4 = _keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output[index++] = chr1;

            if (enc3 != 64)
            {
                output[index++] = chr2;
            }

            if (enc4 != 64)
            {
                output[index++] = chr3;
            }

        }

        return this.bytes2str(output.subarray(0,index));
    }
    // 测试
    let base = "guoke3915";
    var tmp = this.base64_encode(base);
    console.log(tmp);   // 输出: Z3Vva2UzOTE1

    var out = this.base64_decode(tmp);
    console.log(out )     // 输出: guoke3915

异或加密并输出Base64字符串

回到主题,从上面异或加密和Base64编码的方法中,很容易就能找出规律,要合并二个算法,只要在Base64编码的时候对编码时的字节做一个异或操作就可以了.

     /**
      * encrypto 加密程序
      * @param {strng} str 待加密字符串
      * @param {number} xor 异或值
      * @return {string} 加密后并base64编码的字符串
    */
    private base64_xor_encode(input:string, xor: number):string{
        const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        let data = this.str2bytes(input);

        while (i < data.length)
        {
            chr1 = data[i++]^xor;
            chr2 = i < data.length?(data[i++]^xor):undefined;
            chr3 = i < data.length?(data[i++]^xor):undefined;

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2))
            {
                enc3 = enc4 = 64;
            }
            else if (isNaN(chr3))
            {
                enc4 = 64;
            }

            output = output +
                _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
                _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
        }

        return output;
    }

    /**
     * decrypto 解密程序
     * @param {strng} str 待加密字符串
     * @param {number} xor 异或值
     * @return {strng} 解密后的字符串
     */
    private base64_xor_decode(input:string,xor: number):string{
        const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        var output = new Uint8Array(input.length);
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0, index = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < input.length)
        {
            enc1 = _keyStr.indexOf(input.charAt(i++));
            enc2 = _keyStr.indexOf(input.charAt(i++));
            enc3 = _keyStr.indexOf(input.charAt(i++));
            enc4 = _keyStr.indexOf(input.charAt(i++));

            chr1 = ((enc1 << 2) | (enc2 >> 4));
            chr2 = (((enc2 & 15) << 4) | (enc3 >> 2));
            chr3 = (((enc3 & 3) << 6) | enc4);

            output[index++] = chr1^xor;

            if (enc3 != 64)
            {
                output[index++] = chr2^xor;
            }

            if (enc4 != 64)
            {
                output[index++] = chr3^xor;
            }

        }

        return this.bytes2str(output.subarray(0,index));
    }
    // 测试
    let base = "guoke3915";
    var tmp = this.base64_xor_encode(base);
    console.log(tmp);   // 输出: UEJYXFIEDgYC

    var out = this.base64_xor_decode(tmp);
    console.log(out )     // 输出: guoke3915
0%