How do I convert a byte array to Base64 in Java?
Asked Answered
T

4

202

Okay, I know how to do it in C#.

It's as simple as:

Convert.ToBase64String(byte[])
and Convert.FromBase64String(string) to get byte[] back.

How can I do this in Java?

Tisdale answered 10/3, 2010 at 16:14 Comment(0)
D
375

Java 8+

Encode or decode byte arrays:

byte[] encoded = Base64.getEncoder().encode("Hello".getBytes());
println(new String(encoded));   // Outputs "SGVsbG8="

byte[] decoded = Base64.getDecoder().decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

String encoded = Base64.getEncoder().encodeToString("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(Base64.getDecoder().decode(encoded.getBytes()));
println(decoded)    // Outputs "Hello"

For more info, see Base64.

Java < 8

Base64 is not bundled with Java versions less than 8. I recommend using Apache Commons Codec.

For direct byte arrays:

Base64 codec = new Base64();
byte[] encoded = codec.encode("Hello".getBytes());
println(new String(encoded));   // Outputs "SGVsbG8="

byte[] decoded = codec.decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

Base64 codec = new Base64();
String encoded = codec.encodeBase64String("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(codec.decodeBase64(encoded));
println(decoded)    // Outputs "Hello"

Android (with Java < 8)

If you are using the Android SDK before Java 8 then your best option is to use the bundled android.util.Base64.

For direct byte arrays:

byte[] encoded = Base64.encode("Hello".getBytes());
println(new String(encoded))    // Outputs "SGVsbG8="

byte [] decoded = Base64.decode(encoded);
println(new String(decoded))    // Outputs "Hello"

Or if you just want the strings:

String encoded = Base64.encodeToString("Hello".getBytes());
println(encoded);   // Outputs "SGVsbG8="

String decoded = new String(Base64.decode(encoded));
println(decoded)    // Outputs "Hello"

Other: Spring < 6.0.5

If for some reason Java's Base64 isn't suitable and you use Spring, see Base64Utils. This package, however, is deprecated as of 6.0.5 and so I don't recommend.

Desirous answered 5/11, 2015 at 18:40 Comment(3)
On Android, the encode and encodeToString methods also expect a flags parameter, so you need to do Base64.encode("Hello".getBytes(), Base64.DEFAULT); (and the same for encodeToString).Callimachus
great answer, in my opinion must be accepted as correct answer.Nazarius
Base64Utils is now deprecated since Spring 6.0.5 (and is scheduled for removal in incoming 6.2 version) in favor of java.util.Base64.Bots
B
35

Use:

byte[] data = Base64.encode(base64str);

Encoding converts to Base64

You would need to reference commons codec from your project in order for that code to work.

For java8:

import java.util.Base64
Bellaude answered 3/5, 2012 at 3:50 Comment(1)
The methods for encode / decode of class Base64 are static so you don't need to create a new object. Then: byte [] data = Base64.decode(base64str); is enough.Acquaint
M
22

Additionally, for our Android friends (API Level 8):

import android.util.Base64

...

Base64.encodeToString(bytes, Base64.DEFAULT);
Mayce answered 6/6, 2018 at 7:29 Comment(0)
A
8

In case you happen to be using Spring framework along with java, there is an easy way around.

  1. Import the following.

    import org.springframework.util.Base64Utils;
  2. Convert like this.

    byte[] bytearr ={0,1,2,3,4};
    String encodedText = Base64Utils.encodeToString(bytearr);
    

    To decode you can use the decodeToString method of the Base64Utils class.

Astrix answered 15/6, 2018 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.