Search code examples
javac#base64decodeapache-commons

Base64 String decoding issue in Java 7


I got a Base64 encoded string, which I want to decode on Java 7 in my engine. The String is this:

String msg = "TwBuACAAMAAzACAATQBhAHIAIAAxADQALAAgAGEAdAAgADEAOQAyADkAaAByAHMALAAg
    AFMAeQByAGkAYQBuACAAagBlAHQAcwAgAGYAaQByAGUAZAAgADEAOAAgAHIAbwBjAGsAZQB0AHMAIA
    BhAHQAIABBAHIAcwBhAGwAJwBzACAAVwBhAGQAaQAgAEsAaABhAGkAcgAgAGEAbgBkACAAdABoAGUA
    IAByAG8AYQBkACAAYgBlAHQAdwBlAGUAbgAgAFMAbABlAGUAdABhACAAYQBuAGQAIABuAG8AcgB0AG
    gAZQByAG4AIAB0AG8AdwBuACAAbwBmACAAQQByAHMAYQBsAC0AQgBhAGEAbABiAGUAawAgAGEAcgBl
    AGEALgA=";

It comes from a web service written in C#.

If I paste it in any Base64 decoder on the web it works fine. If I use apache commons decoder it will give me weird output, where every letter is followed by a black diamond with questionmark (see sreenshot)Screenshot of Output:

I Have tried to set encodings before decoding and when converting to a String, but with no success. Here is the code:

System.setProperty("file.encoding", "UTF-8");       
String enc =
System.getProperty("file.encoding","UTF-8");
System.out.println(enc);        
String msg = "TwBuACAAMAAzACAATQBhAHIAIAAxADQALAAgAGEAdAAgADEAOQAyADkAaAByAHMALAAg
    AFMAeQByAGkAYQBuACAAagBlAHQAcwAgAGYAaQByAGUAZAAgADEAOAAgAHIAbwBjAGsAZQB0AHMAIA
    BhAHQAIABBAHIAcwBhAGwAJwBzACAAVwBhAGQAaQAgAEsAaABhAGkAcgAgAGEAbgBkACAAdABoAGUA
    IAByAG8AYQBkACAAYgBlAHQAdwBlAGUAbgAgAFMAbABlAGUAdABhACAAYQBuAGQAIABuAG8AcgB0A
    GgAZQByAG4AIAB0AG8AdwBuACAAbwBmACAAQQByAHMAYQBsAC0AQgBhAGEAbABiAGUAawAgAGEAcgB
    lAGEALgA=";

byte[] res = Base64.decodeBase64(msg);      
System.out.println(new String(res, "UTF-8"));

Any ideas?

Kind Regards, Kartopete


Solution

  • The string is in UTF-16LE encoding, according to Oracle's documentation: Sixteen-bit Unicode (or UCS) Transformation Format, little-endian byte order. More information about UTF-16 variants can be found here.

    After converting your base64 string to bytes, do as follows:

    byte[] bytes; // base64 decoded bytes
    String s = new String(bytes, "UTF-16LE");
    

    The value of the string is:

    On 03 Mar 14, at 1929hrs, Syrian jets fired 18 rockets at Arsal's Wadi Khair and the road between Sleeta and northern town of Arsal-Baalbek area.