3

I am getting a base64encoded xml data in webservice response and I want to decode it in Java . I tried org.apache.commons.codec.binary.Base64;

byte bytesEncoded[] = base64encodedStringfromWebservice.getBytes();
// Decrypt data on other side, by processing encoded data
byte[] valueDecoded= Base64.decodeBase64(bytesEncoded );
System.out.println("Decoded value is " + new String(valueDecoded));

but still some characters like (< , />) are not getting decoded properly .

Please suggest how to decode it correctly?

user207421
  • 305,947
  • 44
  • 307
  • 483
Tarun1980
  • 39
  • 1
  • 2

2 Answers2

3

The getBytes method from String is platform specific. You need to specify an encoding, and later use that same encoding to decode the string. You can just use UTF8.

You also need to do the steps in reverse order: string -> base64 -> raw utf8 bytes -> base64 -> string

// ENCODE data
byte bytesEncoded[] = base64encodedStringfromWebservice.getBytes("UTF8");

// DECODE data on other side, by processing encoded data
String base64encodedStringfromWebservice = new String(bytesEncoded, "UTF8");
byte[] valueDecoded = Base64.decodeBase64(base64encodedStringfromWebservice);
System.out.println("Decoded value is " + new String(valueDecoded));
vz0
  • 32,345
  • 7
  • 44
  • 77
1

Try specifiying the charset you are using. for example, utf-8, and as you can see here: Decoding a Base64 string in Java

byte[] valueDecoded= Base64.decodeBase64(bytesEncoded);
System.out.println("Decoded value is " + new String(valueDecoded, "UTF-8"));
Community
  • 1
  • 1
Oldskultxo
  • 945
  • 8
  • 19