【问题标题】:What to do to make base64 decode functions to decode strings in local language?如何使 base64 解码函数以本地语言解码字符串?
【发布时间】:2017-12-27 16:17:31
【问题描述】:

我有这个很久以前从某个地方抓到的函数:

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); -> ×åêáîêñ

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

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

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

我尝试将Encoding.UTF8.GetBytes( 与其他脚本一起使用,但这些脚本仍然停止执行。

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

【问题讨论】:

  • 字符集应设置为 utf-8 并在所有地方设置为相同。这篇文章可能会回答你的问题:stackoverflow.com/questions/2587136/…
  • @jeff - Eww 我这样做了,但唯一改变的是奇怪的符号。现在它们是:ЧекбокÑ。并且我使用Ecoding.UTF8生成base64和写html文件。
  • btoa()用于编码为Base64,用于解码atob()!
  • @Sirko - 呃,我只是搞砸了那些。反正它们对我没用,因为出现在 IE10 中,但我需要支持较低的 IE。
  • 使用你提到的脚本我得到了正确的结果fiddle.jshell.net/leighking2/bxdd7489。检查浏览器实际看到的 js,并确保它正在尝试对字符 Чекбокс 进行编码。如果这被硬编码到 js 中,那么它可能在传输过程中被更改。我在 moment.js 中遇到了翻译混乱的问题,问题是文件以 ISO-8859-1 而不是 UTF-8 的形式交付。

标签: javascript encoding base64


【解决方案1】:

调整后,将 base64 转换为某种本地编码(例如 cp-1251)真的很有趣,而且没有直接的方法。 我分两步完成:

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

代码sn-p:

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);

利润!编码愉快!

【讨论】:

    【解决方案2】:

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

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

    并且代码 incase fiddle 会失败

    /**
    *
    *  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=="));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2011-10-31
      • 1970-01-01
      相关资源
      最近更新 更多