73

I'm trying to decode a simple Base64 string, but am unable to do so. I'm currently using the org.apache.commons.codec.binary.Base64 package.

The test string I'm using is: abcdefg, encoded using PHP YWJjZGVmZw==.

This is the code I'm currently using:

Base64 decoder = new Base64();
byte[] decodedBytes = decoder.decode("YWJjZGVmZw==");
System.out.println(new String(decodedBytes) + "\n") ;   

The above code does not throw an error, but instead doesn't output the decoded string as expected.

Robert
  • 8,717
  • 2
  • 27
  • 34
TomasB
  • 731
  • 1
  • 5
  • 3
  • well, first of all, you should always specify a character set when converting from bytes to chars, perhaps "US-ASCII" for you example string (`new String(decodedBytes, "US-ASCII")`). – jtahlborn Jul 18 '12 at 15:13
  • @jtahlborn the apache commons codec is built to "just work" in system default encoding if you don't specify otherwise – Affe Jul 18 '12 at 15:18
  • @TomasB code you posted works fine for me, is that the full sample? – Affe Jul 18 '12 at 15:18
  • The code you pasted works all right for me. Are you certain that that code snippet actually gets executed at all? Have you added some fixed string println or similar? – MvG Jul 18 '12 at 15:19
  • Your code works fine and prints "abcdefg". – vbezhenar Jul 18 '12 at 15:21
  • 2
    @Affe - i have no idea what you mean by it "just working in the system default encoding". regardless of the library used, when you convert bytes to chars, you should _always_ specify a charset. – jtahlborn Jul 18 '12 at 15:22
  • I tried to add "UTF-8" no change. @erickson - it print nothing :-( – TomasB Jul 18 '12 at 15:24
  • show your entire example program – jtahlborn Jul 18 '12 at 15:26
  • 2
    If it prints nothing, the problem is elsewhere in your program, not with the Base-64 decoding. – erickson Jul 18 '12 at 15:27
  • @TomasB Edited my anwser, this should work. – RTB Jul 18 '12 at 15:56

4 Answers4

79

Modify the package you're using:

import org.apache.commons.codec.binary.Base64;

And then use it like this:

byte[] decoded = Base64.decodeBase64("YWJjZGVmZw==");
System.out.println(new String(decoded, "UTF-8") + "\n");
RTB
  • 5,773
  • 7
  • 33
  • 50
  • Be careful with sun.* packages! http://stackoverflow.com/questions/469695/decode-base64-data-in-java – Diego Agulló Jul 18 '12 at 15:19
  • Sorry I should specified that I have to use org.apache.commons.codec.binary.Base64 because it's a safe url decoder. – TomasB Jul 18 '12 at 15:20
  • 3
    There's javax.xml.bind.DatatypeConverter.parseBase64Binary in JDK so you don't need to use sun API or external libraries to work with base64. – vbezhenar Jul 18 '12 at 15:24
  • @RTB you should always use a characted encoding when constructing a string from a byte[]. Change to e.g. `new String(decoded, "UTF-8")` – maasg Jul 18 '12 at 16:00
  • @TomasB If my anwser helped please accept it so others can learn form it. If not how can i help you further? – RTB Jul 19 '12 at 11:28
  • On Android org.apache.commons.codec.binary.Base64 marked as deprecated. In this case use android.util.Base64 instead. – Ernest Poletaev Jul 05 '16 at 02:15
17

The following should work with the latest version of Apache common codec

byte[] decodedBytes = Base64.getDecoder().decode("YWJjZGVmZw==");
System.out.println(new String(decodedBytes));

and for encoding

byte[] encodedBytes = Base64.getEncoder().encode(decodedBytes);
System.out.println(new String(encodedBytes));
Hany Sakr
  • 2,591
  • 28
  • 27
16

If you don't want to use apache, you can use Java8:

byte[] decodedBytes = Base64.getDecoder().decode("YWJjZGVmZw=="); 
System.out.println(new String(decodedBytes) + "\n");
Denys
  • 1,308
  • 12
  • 13
3

Commonly base64 it is used for images. if you like to decode an image (jpg in this example with org.apache.commons.codec.binary.Base64 package):

byte[] decoded = Base64.decodeBase64(imageJpgInBase64);
FileOutputStream fos = null;
fos = new FileOutputStream("C:\\output\\image.jpg");
fos.write(decoded);
fos.close();
Alberto Perez
  • 1,019
  • 15
  • 17