1

I have created some code that should work:

String input = "PONT4uYmTYwiP0omcAxZG8a3vKI=";
String expectedOut = "3ce353e2e6264d8c223f4a26700c591bc6b7bca2";

Base64.Decoder decoder = Base64.getDecoder();
byte[] outAsByte = decoder.decode(input);
String output = new String(outAsByte, StandardCharsets.UTF_8);

System.out.println(input);
System.out.println(output);

(based on How do I decode a base64 encoded string? - I know that it's in C#)

Surprisingly this outputs the following:

PONT4uYmTYwiP0omcAxZG8a3vKI=
<?S??&M?"?J&pY???

Why does this print a bunch of gibberish instead of the the expected output?

Edit: I got the expected output using this bash command:

echo -n PONT4uYmTYwiP0omcAxZG8a3vKI= | base64 -d | xxd -p
Community
  • 1
  • 1
J Atkin
  • 3,080
  • 2
  • 19
  • 33

3 Answers3

3

From xxd's man page

xxd creates a hex dump of a given file or standard input.

You're taking a hex dump for the decoded value.

You can reproduce it with the utility provided here

System.out.println(Hex.encodeHexString(outAsByte));

generates

3ce353e2e6264d8c223f4a26700c591bc6b7bca2

which matches your

                  //  3ce353e2e6264d8c223f4a26700c591bc6b7bca2
String expectedOut = "3ce353e2e6264d8c223f4a26700c591bc6b7bca2";
Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

How did you come up with the expected outcome?

Decoding your input using base64 and UTF-8 results in <ãSâæ&M"?J&pYÆ·¼¢, which is your actual outcome, apart from the UTF-8 specific characters.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
  • I used `echo -n PONT4uYmTYwiP0omcAxZG8a3vKI= | base64 -d | xxd -p`, If you don't have bash try it here: http://www.tutorialspoint.com/execute_bash_online.php. The [`xxd`](http://linux.about.com/library/cmd/blcmdl1_xxd.htm) command seems to be where the magic is happening, although I have no idea what it is doing... My first guess was that it was doing some sort of text conversion, but that seems wrong. – J Atkin May 29 '15 at 23:35
1

0x3c == '<' etc.

The expected output should really be 0x3c 0xe3 ...

ZhongYu
  • 19,446
  • 5
  • 33
  • 61