[英]How to decode a Base64 string in Scala or Java?
I have a string encoded in Base64:我有一个用 Base64 编码的字符串:
eJx9xEERACAIBMBKJyKDcTzR_hEsgOxjAcBQFVVNvi3qEsrRnWXwbhHOmzWnctPHPVkPu-4vBQ==
How can I decode it in Scala language?我怎样才能用 Scala 语言解码它?
I tried to use:我尝试使用:
val bytes1 = new sun.misc.BASE64Decoder().decodeBuffer(compressed_code_string)
But when I compare the byte array with the correct one that I generated in Python language, there is an error.但是当我将字节数组与我用 Python 语言生成的正确数组进行比较时,出现错误。 Here is the command I used in python:
这是我在python中使用的命令:
import base64
base64.urlsafe_b64decode(compressed_code_string)
The Byte Array in Scala is: Scala 中的字节数组是:
(120, -100, 125, -60, 65, 17, 0, 32, 8, 4, -64, 74, 39, 34, -125, 113, 60, -47, -2, 17, 44, -128, -20, 99, 1, -64, 80, 21, 85, 77, -66, 45, -22, 18, -54, -47, -99, 101, -16, 110, 17, -50, -101, 53, -89, 114, -45, -57, 61, 89, 15, -69, -2, 47, 5)
And the one generated in python is:在 python 中生成的一个是:
(120, -100, 125, -60, 65, 17, 0, 32, 8, 4, -64, 74, 39, 34, -125, 113, 60, -47, -2, 17, 44, -128, -20, 99, 1, -64, 80, 21, 85, 77, -66, 45, -22, 18, -54, -47, -99, 101, -16, 110, 17, -50, -101, 53, -89, 114, -45, -57, 61, 89, 15, -69, -18, 47, 5)
Note that there is a single difference in the end of the array请注意,数组末尾有一个区别
In Scala, Encoding a String to Base64 and decoding back to the original String using Java APIs:在 Scala 中,将字符串编码为 Base64 并使用 Java API 解码回原始字符串:
import java.util.Base64
import java.nio.charset.StandardCharsets
scala> val bytes = "foo".getBytes(StandardCharsets.UTF_8)
bytes: Array[Byte] = Array(102, 111, 111)
scala> val encoded = Base64.getEncoder().encodeToString(bytes)
encoded: String = Zm9v
scala> val decoded = Base64.getDecoder().decode(encoded)
decoded: Array[Byte] = Array(102, 111, 111)
scala> val str = new String(decoded, StandardCharsets.UTF_8)
str: String = foo
There is unfortunatelynot just one Base64 encoding .不幸的是,Base64 编码不止一种。 The
-
character doesn't have the same representation in all encodings. -
字符在所有编码中都没有相同的表示。 For example, in the MIME encoding, it's not used at all.例如,在 MIME 编码中,它根本没有使用。 In the encoding for URLs, it is a value of 62--and this is the one that Python is using.
在 URL 的编码中,它的值为 62——这是 Python 正在使用的值。 The default sun.misc decoder wants
+
for 62. If you change the -
to +
, you get the correct answer (ie the Python answer).默认的 sun.misc 解码器需要
+
为 62。如果将-
更改为+
,则会得到正确答案(即 Python 答案)。
In Scala, you can convert the string s
to MIME format like so:在 Scala 中,您可以将字符串
s
转换为 MIME 格式,如下所示:
s.map{ case '-' => '+'; case '_' => '/'; case c => c }
and then the Java MIME decoder will work.然后 Java MIME 解码器将工作。
Both Python and Java are correct in terms of the decoding. Python 和 Java 在解码方面都是正确的。 They are just using a different RFC for this purpose.
他们只是为此使用了不同的 RFC。 Python library is using
RFC 3548
and the used java library is using RFC 4648
and RFC 2045
. Python 库使用
RFC 3548
,使用的 java 库使用RFC 4648
和RFC 2045
。
Changing the hyphen(-) into a plus(+) from your input string will make the both decoded byte data are similar.将输入字符串中的连字符 (-) 更改为加号 (+) 将使两个解码的字节数据相似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.