Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java decoding base64 String

I realise this is probably more of a general java question, but since it's running in Notes\ Domino environment, thought I'd check that community first.

Summary:

I don't seem to be able to decode the string: dABlAHMAdAA= using lotus.domino.axis.encoding.Base64 or sun.misc.BASE64Decoder

I know the original text is: test

I confirmed by decoding at http://www5.rptea.com/base64/ it appears it is UTF-16.

As simple test, using either of below:

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 String as UTF-8

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

it outputs:
t
no error is thrown, but it doesn't complete the code, doesn't get to the "FINISH".

Detail

I'm accessing an asmx web service, in the SOAP response, some nodes contain base64 encoded data. At this point in time, there is no way to get the service changed, so I am having to XPath and decode myself. Encoded data is either text or html. If I pass the encoded data thru http://www5.rptea.com/base64/ and select UTF-16, it decodes correctly, so I must be doing something incorrectly.

As side note, I encoded "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 to:
dGVzdA== ...which if you then feed into 2 decoders above, as expected, decodes correctly.

If I go to site above, and encode "test" as UTF-16, I get: dABlAHMAdAA= so that confirms that data is in UTF-16.

It's like the data is genuine base64 data, but the decoder doesn't recognise it as such. I'm slightly stumped at the moment.

Any pointers or comments would be gratefully received.

like image 874
nick wall Avatar asked Dec 15 '22 08:12

nick wall


1 Answers

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

s_decoded = new String(byte_base64, "UTF-16LE");
like image 129
Joni Avatar answered Jan 10 '23 16:01

Joni