Java中的base64字符串解码
我正在使用Java,并且我有一个我希望解码的base64编码字符串,然后进行一些操作进行转换。
正确的解码值是通过函数 ATOB()
在JavaScript中获得的,但是在Java中,使用 base64.decodebase64()
我无法获得同等值。
示例:
for:
String str = "AAAAAAAAAAAAAAAAAAAAAMaR+ySCU0Yzq+AV9pNCCOI="
使用JavaScript ATOB(str)
我获得 - >
“ plage
new String(base64.decodebase64(str)) i got-
“æ?us
? >“ $” 符号。
当前代码:
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
String script2 = "function decoMemo(memoStr){ print(atob(memoStr).split('')" +
".map((aChar) => `0${aChar.charCodeAt(0).toString(16)}`" +
".slice(-2)).join('').toUpperCase());}";
try {
engine.eval(script2);
Invocable inv = (Invocable) engine;
String returnValue = (String)inv.invokeFunction("decoMemo", memoTest );
System.out.print("\n result: " + returnValue);
} catch (ScriptException | NoSuchMethodException e1) {
e1.printStackTrace();
任何帮助将不胜感激。我搜索很多地方,但找不到正确的答案。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BTOA
被打破,不应使用。问题是,字节不是字符。 base64编码只能做一件事。它将字节转换为几乎所有基于文本的传输机制的字符流。 base64解码会反向做一件事,它将此类字符转换为 bytes 。
令人困惑的是,您正在打印这些字节,就好像它们是字符一样。他们不是。
您最终会以完全相同的字节,但是JavaScript和Java不同意应该如何将其变成Ersatz字符串,因为您正在尝试将其打印到控制台上。这是一个错误 - 字节不是字符。因此,正在使用某种形式的Charset编码,您不希望任何此类编码,因为这些字符显然并不是要像这样打印。
JavaScript有点半配件字符和字节,并将一个随机转换为另一个字符,选择一些随机编码。钱币。在这方面,JavaScript很烂,这就是它。 说明为什么不应该使用它。您正在遇到 问题。
不完全确定如何在JavaScript中修复它 - 但也许您不需要它。 Java和JavaScript一样,很好地解码了字节,但是JavaScript随后将这些字节变成了一些愚蠢的方式,这导致了问题。
btoa
is broken and shouldn't be used.The problem is, bytes aren't characters. Base64 encoding does only one thing. It converts bytes to a stream of characters that survive just about any text-based transport mechanism. And Base64 decoding does that one thing in reverse, it converts such characters into bytes.
And the confusion is, you're printing those bytes as if they are characters. They are not.
You end up with the exact same bytes, but javascript and java disagree on how you're supposed to turn that into an ersatz string because you're trying to print it to a console. That's a mistake - bytes aren't characters. Thus, some sort of charset encoding is being used, and you don't want any of this, because these characters clearly aren't intended to be printed like that.
Javascript sort of half-equates characters and bytes and will freely convert one to the other, picking some random encoding. Oof. Javascript sucks in this regard, it is what it is. The MDN docs on
btoa
explains why you shouldn't use it. You're running into that problem.Not entirely sure how you fix it in javascript - but perhaps you don't need it. Java is decoding the bytes perfectly well, as is javascript, but javascript then turns those bytes into characters into some silly fashion and that's causing the problem.
您拥有的根本没有文本字符串。赠品是一开始的AA。这些映射到许多零字节。在任何标准字符集中,这都不会将其转化为有意义的文本。
因此,您拥有的最有可能是二进制数据。将其转换为字符串不会给您有意义的文本。
现在要解释您在Java和JavaScript之间看到的区别。在我看来,Java和JavaScript都在做出“最佳努力”尝试将二进制数据转换好,好像是在ISO-8859-1中编码的(又称ISO Latin-1)。
问题是一些字节代码正在映射到未分配的代码。
?
,无论是创建字符串时还是输出时。为了记录,这就是在线base64解码器上以上的方式:
未分配的代码为0x91 0x82和0x93。 0x15和0x0b是非打印控制代码。
但最重要的是,您不应将此数据转换为Java或JavaScript中的字符串。应该将其视为二进制;即一系列字节值。
What you have there is not a text string at all. The giveaway is the AA's at the beginning. Those map to a number of zero bytes. That doesn't translate to meaningful text in any standard character set.
So what you have there is most likely binary data. Converting it to a string is not going to give you meaningful text.
Now to explain the difference you are seeing between Java and Javascript. It looks to me as if both Java and Javascript are making a "best effort" attempt to convert the binary data as if is was encoded in ISO-8859-1 (aka ISO LATIN-1).
The problem is some of the bytes codes are mapping to unassigned codes.
?
, either when the string is created or when it is being output.For the record, this is how an online base64 decoder the above for me:
The unassigned codes are 0x91 0x82 and 0x93. 0x15 and 0x0B are non-printing control codes.
But the bottom line is that you should not be converting this data into a string in either Java or in Javascript. It should be treated as binary; i.e. an array of byte values.