Base64 java decoding String

I understand that this is probably a more general Java issue, but since it works in the Notes \ Domino environment, I thought I checked this community first.

Summary:

It seems I can not decode the string: dABlAHMAdAA = using lotus.domino.axis.encoding.Base64 or sun.misc.BASE64Decoder

I know the source: test

I confirmed by decoding at http://www5.rptea.com/base64/ , it looks like UTF-16.

As a simple test using any of the following:

String s_base64 = "dABlAHMAdAA="; byte[] byte_base64 = null; String s_decoded = ""; byte_base64 = new sun.misc.BASE64Decoder().decodeBuffer(s_base64); s_decoded = new String(byte_base64, "UTF-16"); System.out.println("Test1: " + s_decoded); byte_base64 = lotus.domino.axis.encoding.Base64.decode(s_base64); s_decoded = new String(byte_base64, "UTF-16"); System.out.println("Test2: " + s_decoded); System.out.println("========= FINISH."); 

I get the output:
Test1: ???? Test2:

If I create a String as UTF-8

 s_decoded = new String(byte_base64, "UTF-8"); 

output:
t
the error is not thrown, but the code does not complete, does not reach "FINISH".

Detail

I am accessing the asmx web service, in the SOAP response some nodes contain base64 encoded data. There is currently no way to change the service, so I need XPath and decrypt myself. The encoded data is text or html. If I transfer the encoded data through http://www5.rptea.com/base64/ and select UTF-16, it decodes correctly, so I have to do something wrong.

As a side note, I encoded a β€œtest”:

 s_base64 = lotus.domino.axis.encoding.Base64.encode(s_text.getBytes()); System.out.println("test1 encodes to: " + s_base64); s_base64 = new sun.misc.BASE64Encoder().encode(s_text.getBytes()); System.out.println("test2 encodes to: " + s_base64); 

they both encode:
dGVzdA == ... which, if you then feed 2 decoders above, as expected, decodes correctly.

If I go to the site above and encode the β€œtest” as UTF-16, I get: dABlAHMAdAA = to confirm that the data is in UTF-16.

This is similar to the fact that the data is genuine base64 data, but the decoder does not recognize them as such. I'm a bit stumped.

Any pointers or comments would be greatly appreciated.

+4
source share
2 answers

The string was encoded in UTF-16LE (little-endian), where the least significant high byte is stored first. Java defaults to big-endian. You need to use:

 s_decoded = new String(byte_base64, "UTF-16LE"); 
+11
source

I used your sample "dABlAHMAdAA =" in my online base64 decoding tool and it seems like you are missing the base64 Apache jar files Click the link below.

http://www.hosting4free.info/Base64Decode/Base64-Decode.jsp

Site Code -

 import org.apache.commons.codec.binary.Base64; public class base64decode { public static void main(String[] args) throws UnsupportedEncodingException { byte[] decoded = Base64.decodeBase64("YWJjZGVmZw==".getBytes()); System.out.println(new String(decoded) + "\n"); } } 
0
source

Source: https://habr.com/ru/post/1493226/


All Articles