简体   繁体   English

无法在 JavaScript 中解码 base64

[英]Failed to decode base64 in javascript

I receive an id_token as part of my current href.我收到一个id_token作为我当前 href 的一部分。 It is encoded in base64.它以 base64 编码。 I try to decode it using atob(extractedIdToken) , but get the following error:我尝试使用atob(extractedIdToken)对其进行解码,但出现以下错误:

Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded无法在“窗口”上执行“atob”:要解码的字符串未正确编码

When I copy and paste the extracted id_token in my code and go to an online decoding site, it decodes correctly.当我将提取的id_token复制并粘贴到我的代码中并转到在线解码站点时,它会正确解码。 Do you have suggestion?你有什么建议吗?

I always use this to decode and encode in Base64, try it我总是用它在Base64中解码和编码,试试吧

var Base64 = {
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    encode: function (e) {
        var t = "";
        var n, r, i, s, o, u, a;
        var f = 0;
        e = Base64._utf8_encode(e);
        while (f < e.length) {
            n = e.charCodeAt(f++);
            r = e.charCodeAt(f++);
            i = e.charCodeAt(f++);
            s = n >> 2;
            o = (n & 3) << 4 | r >> 4;
            u = (r & 15) << 2 | i >> 6;
            a = i & 63;
            if (isNaN(r)) {
                u = a = 64
            } else if (isNaN(i)) {
                a = 64
            }
            t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
        }
        return t
    },
    decode: function (e) {
        var t = "";
        var n, r, i;
        var s, o, u, a;
        var f = 0;
        e = e.replace(/[^A-Za-z0-9+/=]/g, "");
        while (f < e.length) {
            s = this._keyStr.indexOf(e.charAt(f++));
            o = this._keyStr.indexOf(e.charAt(f++));
            u = this._keyStr.indexOf(e.charAt(f++));
            a = this._keyStr.indexOf(e.charAt(f++));
            n = s << 2 | o >> 4;
            r = (o & 15) << 4 | u >> 2;
            i = (u & 3) << 6 | a;
            t = t + String.fromCharCode(n);
            if (u != 64) {
                t = t + String.fromCharCode(r)
            }
            if (a != 64) {
                t = t + String.fromCharCode(i)
            }
        }
        t = Base64._utf8_decode(t);
        return t
    },
    _utf8_encode: function (e) {
        e = e.replace(/rn/g, "n");
        var t = "";
        for (var n = 0; n < e.length; n++) {
            var r = e.charCodeAt(n);
            if (r < 128) {
                t += String.fromCharCode(r)
            } else if (r > 127 && r < 2048) {
                t += String.fromCharCode(r >> 6 | 192);
                t += String.fromCharCode(r & 63 | 128)
            } else {
                t += String.fromCharCode(r >> 12 | 224);
                t += String.fromCharCode(r >> 6 & 63 | 128);
                t += String.fromCharCode(r & 63 | 128)
            }
        }
        return t
    },
    _utf8_decode: function (e) {
        var t = "";
        var n = 0;
        var r = c1 = c2 = 0;
        while (n < e.length) {
            r = e.charCodeAt(n);
            if (r < 128) {
                t += String.fromCharCode(r);
                n++
            } else if (r > 191 && r < 224) {
                c2 = e.charCodeAt(n + 1);
                t += String.fromCharCode((r & 31) << 6 | c2 & 63);
                n += 2
            } else {
                c2 = e.charCodeAt(n + 1);
                c3 = e.charCodeAt(n + 2);
                t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
                n += 3
            }
        }
        return t
    }
};

Thanks all for your answers.谢谢大家的回答。 I ended up moving the Base64 decoding process to the Java backend using the java package: java.util.Base64.我最终使用 java 包 java.util.Base64 将 Base64 解码过程移至 Java 后端。

In my case, the issue was that the encoded string should have been treated as segments.就我而言,问题是编码的字符串应该被视为段。 Just separate the code with the separator "."只需用分隔符“.”分隔代码即可。 or whatever your case is.或者不管你的情况是什么。 Then, decode only the different parts separately.然后,分别解码不同的部分。 This resolved my issue and I was able to decode using window.atob().这解决了我的问题,我能够使用 window.atob() 进行解码。 Hope this helps.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM