Base64 解码在 Java 和 Ruby 中给出不同的结果
我正在使用解码一些文本 Base64。我尝试了三种不同的 Java 中的库(常见的 Base64
、Java.misc.
以及 java.mail)以及所有库 其中产生相同的结果 下面的文字,这是不正确的。
然而,当我使用 Ruby 解码下面的字符串时,我得到了不同的输出。我使用 Ruby 得到了正确的结果。 Ruby 代码为 print Base64.decode64('
,字符串为
RkxWAQEAAAAJAAAAABIAAK4AAAAAAAAAAgAKb25NZXRhRGF0YQgAAAAHAAV3aWR0aABAdAAAAAAA
AAAGaGVpZ2h0AEB0AAAAAAAAAAlmcmFtZXJhdGUAQBAAAAAAAAAADHZpZGVvY29kZWNpZABACAAA
AAAAAAAMY2FuU2Vla1RvRW5kAQEAD21ldGFkYXRhY3JlYXRvcgIAKVNpbXBsZUZMVldyaXRlci5h
cyB2MC44IHplcm9wb2ludG5pbmUuY29tAAAJ
输出应以 FLV 开头。我不确定我缺少什么以及为什么使用 Java 的输出不同。
I am decoding some text using
Base64. I have tried three different
libraries (commons Base64
, Java.misc.
, and also java.mail) in Java and all
of them produce same result for the
following text, which is not right.
However when I use Ruby to decode the below string I get different output. I get the right result using Ruby.
The Ruby code is print Base64.decode64('<Below String>')
, the string is
RkxWAQEAAAAJAAAAABIAAK4AAAAAAAAAAgAKb25NZXRhRGF0YQgAAAAHAAV3aWR0aABAdAAAAAAA
AAAGaGVpZ2h0AEB0AAAAAAAAAAlmcmFtZXJhdGUAQBAAAAAAAAAADHZpZGVvY29kZWNpZABACAAA
AAAAAAAMY2FuU2Vla1RvRW5kAQEAD21ldGFkYXRhY3JlYXRvcgIAKVNpbXBsZUZMVldyaXRlci5h
cyB2MC44IHplcm9wb2ludG5pbmUuY29tAAAJ
The output should start with FLV. I am not sure what I am missing and why the output is different using Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Base64 解码的结果是二进制数据。您不应该真正尝试将其像文本一样打印。
在不了解 Ruby 的情况下,我希望调用 Base64.decode64 的结果是某种字节数组……并且可以通过多种方式将其转换为文本。
查看返回内容的字节以确定其是否正确。
(不幸的是,据我所知,文档 for Base64.decode64 给出了您正在做的事情的示例 - 将 Base64 解码操作的结果视为文本 目前尚不清楚数据是什么类型。 实际上返回了。这就是为什么我仍然喜欢静态类型语言......)
The result of decoding base64 is binary data. You shouldn't really try to print it as if it were text.
Without knowing Ruby, I'd expect the result of calling
Base64.decode64
to be some sort of byte array... and that could be converted into text in any number of ways.Look at the bytes of what's returned to find out whether or not it's correct.
(It's unfortunate that as far as I can see, the documentation for Base64.decode64 gives examples of exactly the kind of thing you're doing - treating the result of a base64 decode operation as text. It's not clear what type of data is actually returned. This sort of thing is why I still like statically typed languages...)
你想打印成文本吗?这是行不通的,请尝试使用 ByteArray 来存储解码后的字符串。
例子:
Are you trying to print out into Text? that won't work, try using a ByteArray to store the decoded string.
Example:
解码后的字符串实际上以“FLV”开头:
JRuby 1.6.1
The decoded string actually starts with 'FLV':
JRuby 1.6.1