如何使base64解码函数以本地语言解码字符串?

问题描述 投票:0回答:2

我有很久以前从某个地方抓过的这个功能:

function decode_base64(s)
{
    var e = {}, i, b = 0, c, x, l = 0, a, r = '', w = String.fromCharCode, L = s.length;
    var A = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    for(i = 0; i < 64; i++)
        e[A.charAt(i)] = i;

    for(x = 0; x < L; x++)
    {
        c = e[s.charAt(x)];
        b = (b << 6) + c;
        l += 6;

        while(l >= 8)
            ((a = (b >>> (l -= 8)) & 0xff) || (x < (L - 2))) && (r += w(a));
    }

    return r;
};

在我的C#应用​​程序中,我编码这样的字符串:Convert.ToBase64String(Encoding.Default.GetBytes(str));

然后我生成嵌入这些字符串的html文件。

然后JS脚本解码它:decode_base64(str);

一切都很好用英文字母,但不是当地的。它们解码成奇怪的符号。

Convert.ToBase64String(Encoding.Default.GetBytes("HTML_SubunitsNavigator0")); decode_base64(str); -> HTML_SubunitsNavigator0

Convert.ToBase64String(Encoding.Default.GetBytes("Чекбокс")); //my native language decode_base64(str); -> ×åêáîêñ

我将页面编码设置为<meta http-equiv = 'Content-Type' content = 'text/html; charset = windows-1251' />,但这没有帮助。

我尝试使用其他base64解码函数,例如:http://www.webtoolkit.info/javascript-base64.html但是出现了,那些内部发生了一些错误,脚本停止执行我调用解码方法的地方。

我尝试了btoa,但它解码为......另一个“base64 lookable”序列。

我尝试将Encoding.UTF8.GetBytes(与其他脚本一起使用,但无论如何都会停止执行。

我能做些什么以某种方式正确解码base64与母语?

javascript encoding base64
2个回答
0
投票

使用你提到的脚本我得到正确的结果http://fiddle.jshell.net/leighking2/bxdd7489/(这是在js编码和解码)

在你的情况下,"0J3QvtCy0YvQuSDRjtC90LjRgg=="硬编码到脚本中确实产生了Новыйюнит。 http://fiddle.jshell.net/leighking2/bxdd7489/3/

并且代码变得迟钝

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(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 +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

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

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}


console.log( Base64.decode("0J3QvtCy0YvQuSDRjtC90LjRgg=="));

0
投票

调整出来,将base64转换为某种本地编码(例如cp-1251)非常有趣,并且没有简单易行的方法。我分两步完成:

1)使用例如此script将base64转换为字节数组 2)使用本机Web API的TextDecoder(或某些polyfill)将字节数组转换为您想要的任何内容

代码段:

import encoding from 'text-encoding'; // polyfill
import { default as Base64Binary } from 'utils/base64'; // made the script importable

const win1251decoder = new encoding.TextDecoder('windows-1251');
const uint8Array = Base64Binary.decode(myXmlInBase64); //windows-1251 - originally
const xmlFile = win1251decoder.decode(uint8Array);

利润!快乐的编码!

© www.soinside.com 2019 - 2024. All rights reserved.