4

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.

nick wall
  • 161
  • 2
  • 13

2 Answers2

11

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");
Joni
  • 108,737
  • 14
  • 143
  • 193
  • If I could jump thru the screen and give you a hug, I would. I have been flailing around with this for hours. Many, many thanks. – nick wall Jul 24 '13 at 15:14
  • 1
    new String(byte_base64 , StandardCharsets.UTF_16LE) is possible after Java 7 – naveejr Aug 20 '14 at 05:15
0

i have used your sample "dABlAHMAdAA=" on my base64 decode online tool and it seems like you are missing the Apache base64 jar files Click the link below.

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

The code behind the website is

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");

}

}