3

I have some encoded string into base64 "AhSld52dYtRQJDddAT4XVrymbrei6G03FFNvns6d1"

Was trying to decode it and have strange output symbols "P�,"����Ej��s�"

String result = "AhSld52dYtRQJDddAT4XVrymbrei6G03FFNvns6d1";
byte[] decoded= Base64.decodeBase64(result.getBytes());
Arrays.toString(decoded);  --> [2, 20, -91, 119, -99, -99, 98, -44, 80, 36, 55, 93]
new String(decoded);  --> P�,"����Ej��s��
Diana Meissen
  • 153
  • 3
  • 10
  • 3
    What should result be? Looks like wrong encoding. – talex Jun 11 '19 at 09:20
  • 1
    What are you using as a library, what is the source of Base64 class? As this does not look like https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html – maslan Jun 11 '19 at 09:22
  • Possible duplicate of [How to convert byte array to string and vice versa?](https://stackoverflow.com/questions/1536054/how-to-convert-byte-array-to-string-and-vice-versa) – Ceiling Gecko Jun 11 '19 at 09:27
  • @maslan, in this example used org.apache.commons.codec.binary.Base64, and was trying with java.util.Base64; in this way -> byte[] decoded = Base64.getDecoder().decode(result); both of this libraries gives the same values of byte array [2, 20, -91, 119, -99, -99, 98, -44, 80, 36, 55, 93] – Diana Meissen Jun 11 '19 at 09:38
  • @talex, result sould be something like "uniqueServiceId="2345", someArguments="1234"" – Diana Meissen Jun 11 '19 at 09:38
  • @Ceiling_Gecko, no it is not the same, here i have come decoding or encoding problems – Diana Meissen Jun 11 '19 at 09:38

1 Answers1

1

Base64 is used to encode binary data to ASCII and vice versa. The binary data need not be a valid string, so trying to create a String from some possibly random bytes will most likely fail and give you "garbage" as a result.

In other words, you should not try to interpret the byte[] as a String, unless you know from some side channel, that it contains string data.

I suggest you take a look at what is base64

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82