简体   繁体   English

使用 Java/Kotlin Base64 解码器解码 JavaScript btoa() 编码的字符串

[英]Decode JavaScript btoa() encoded string using Java/Kotlin Base64 decoder

I have an encoded string as follows:我有一个编码字符串如下:

S0VEQUkgUlVOQ0lUIFZSSU5FIEVOVEVSUFJJU0UgLSBLRyBTSU1QQU5HIDQgU09PSw

This was encoded using JavaScript btoa() function. This string can be correctly decoded using JavaScript atob() function. It should give the following decoded string:这是使用 JavaScript btoa() function 编码的。这个字符串可以使用 JavaScript atob() function 正确解码。它应该给出以下解码字符串:

KEDAI RUNCIT VRINE ENTERPRISE - KG SIMPANG 4 SOOK

Right now I'm developing Android app and I have to decode this string, I'm using the following to decode:现在我正在开发 Android 应用程序,我必须解码这个字符串,我使用以下代码进行解码:

java.util.Base64.getDecoder().decode(encodedstring).toString();

I'm not getting the correct decoded output. Anyone knows what's the problem?我没有得到正确的解码 output。有人知道问题出在哪里吗? Is it even possible to decode using Java?甚至可以使用 Java 解码吗?

decode(String) returns a byte[] , you need to convert that to a string using a String constructor and not the toString() method: decode(String)返回一个byte[] ,您需要使用String构造函数而不是toString()方法将其转换为字符串:

byte[] bytes = java.util.Base64.getDecoder().decode(encodedstring);
String s = new String(bytes, java.nio.charset.StandardCharsets.UTF_8);

It looks like you need mime decoder message看来您需要 MIME 解码器消息

java.util.Base64.Decoder decoder = java.util.Base64.getMimeDecoder();  
// Decoding MIME encoded message  
String dStr = new String(decoder.decode(encodedstring));  
System.out.println("Decoded message: "+dStr);   

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

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