简体   繁体   中英

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) 输出屏幕截图 :

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

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.

my output in eclipse,

String msg = "TwBuACAAMAAzACAATQBhAHIAIAAxADQALAAgAGEAdAAgADEAOQAyADkAaAByAHMALAAgAFMAeQByAGkAYQBuACAAagBlAHQAcwAgAGYAaQByAGUAZAAgADEAOAAgAHIAbwBjAGsAZQB0AHMAIABhAHQAIABBAHIAcwBhAGwAJwBzACAAVwBhAGQAaQAgAEsAaABhAGkAcgAgAGEAbgBkACAAdABoAGUAIAByAG8AYQBkACAAYgBlAHQAdwBlAGUAbgAgAFMAbABlAGUAdABhACAAYQBuAGQAIABuAG8AcgB0AGgAZQByAG4AIAB0AG8AdwBuACAAbwBmACAAQQByAHMAYQBsAC0AQgBhAGEAbABiAGUAawAgAGEAcgBlAGEALgA=";
byte[] b = Base64.decodeBase64(msg);
System.out.println(new String(b));

Output

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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