Base64 Encoding and Decoding in Java
This post will discuss Base64 Encoding and Decoding using plain Java, Guava, and Apache Commons.
Base64 is a group of similar binary-to-text encoding schemes representing binary data in an ASCII string format by translating it into a radix-64 representation. Each Base64 digit represents exactly 6-bits of data that means 3 bytes can be represented by 4 6-bit Base64 digits.
Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remain intact without modification during transport. Base64 is commonly used in a number of applications, including email via MIME and storing complex data in XML.
1. Using Java 8
Java 8 finally provided support for Base64 Encoding and Decoding capabilities by providing Base64, Base64.Encoder and Base64.Decoder utility class. The Base64 class consists of static methods for obtaining instances of encoders (Base64.Encoder) and decoders (Base64.Decoder) for the Base64 encoding scheme.
The following program uses Base64.Encoder.encodeToString() method for encoding the specified byte array into a Base64 encoded string and Base64.Decoder.decode() method for decoding back the Base64 encoded string into a newly allocated byte array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Base64; // Base64 encoding and decoding in Java class Main { public static void main(String[] args) { String string = "Techie Delight"; // encode a string using `Base64` encoder Base64.Encoder encoder = Base64.getEncoder(); String encoded = encoder.encodeToString(string.getBytes()); System.out.println("Encoded Data: " + encoded); // decode the encoded data Base64.Decoder decoder = Base64.getDecoder(); String decoded = new String(decoder.decode(encoded)); System.out.println("Decoded Data: " + decoded); } } |
Output:
Encoded Data: VGVjaGllIERlbGlnaHQ=
Decoded Data: Techie Delight
Instead of writing the resulting bytes to a string, we can also use a byte array by using the Base64.Encoder.encode() method, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.Base64; // Base64 encoding and decoding in Java class Main { public static void main(String[] args) { String string = "Techie Delight"; // encode a string using `Base64` encoder Base64.Encoder encoder = Base64.getEncoder(); byte[] encodedBytes = encoder.encode(string.getBytes()); System.out.println("Encoded Data: " + new String(encodedBytes)); // decode the encoded data Base64.Decoder decoder = Base64.getDecoder(); byte[] decodedBytes = decoder.decode(encodedBytes); System.out.println("Decoded Data: " + new String(decodedBytes)); } } |
Output:
Encoded Data: VGVjaGllIERlbGlnaHQ=
Decoded Data: Techie Delight
2. Using javax.xml.bind.DatatypeConverter
The DatatypeConverter class from javax.xml.bind package has two methods – printBase64Binary() and parseBase64Binary(), which can be used to convert an array of bytes into a string containing a lexical representation of xsd:base64Binary and then converting that string back into an array of bytes.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import javax.xml.bind.DatatypeConverter; // Base64 encoding and decoding in Java class Main { public static void main(String[] args) { String string = "Techie Delight"; // encode a string using `Base64` encoder String encoded = DatatypeConverter.printBase64Binary(string.getBytes()); System.out.println("Encoded String: " + encoded); // decode the encoded data String decoded = new String(DatatypeConverter.parseBase64Binary(encoded)); System.out.println("Decoded String: " + decoded); } } |
Output:
Encoded String: VGVjaGllIERlbGlnaHQ=
Decoded String: Techie Delight
3. Using Guava Library
One can also use Guava’s BaseEncoding class for reversibly translating between the byte sequence and printable ASCII strings using the Base64 encoding.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.google.common.io.BaseEncoding; // Base64 encoding and decoding in Java class Main { public static void main(String[] args) { String string = "Techie Delight"; // encode a string using `Base64` encoder String encoded = BaseEncoding.base64().encode(string.getBytes()); System.out.println("Encoded String: " + encoded); // decode the encoded data String decoded = new String(BaseEncoding.base64().decode(encoded)); System.out.println("Decoded String: " + decoded); } } |
Output:
Encoded String: VGVjaGllIERlbGlnaHQ=
Decoded String: Techie Delight
4. Using Apache Commons Library
Apache Commons also provides an implementation of the Base64 algorithm. The following code creates a Base64 codec, which is then used for decoding and encoding the specified data.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import org.apache.commons.codec.binary.Base64; // Base64 encoding and decoding in Java using Apache Commons class Main { public static void main(String[] args) { String string = "Techie Delight"; Base64 base64 = new Base64(); // encode a string using `Base64` encoder byte[] encodedBytes = base64.encode(string.getBytes()); System.out.println("Encoded Data: " + new String(encodedBytes)); // decode the encoded data String decodedString = new String(base64.decode(encodedBytes)); System.out.println("Decoded Data: " + decodedString); } } |
Output:
Encoded Data: VGVjaGllIERlbGlnaHQ=
Decoded Data: Techie Delight
We can also use static methods provided by Apache Commons’s Base64 class – encodeBase64() and decodeBase64().
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import org.apache.commons.codec.binary.Base64; // Base64 encoding and decoding in Java using Apache Commons Library class Main { public static void main(String[] args) { String string = "Techie Delight"; // encode a string using `Base64` encoder byte[] encodedBytes = Base64.encodeBase64(string.getBytes()); System.out.println("Encoded Data: " + new String(encodedBytes)); // decode the encoded data String decodedString = new String(Base64.decodeBase64(encodedBytes)); System.out.println("Decoded Data: " + decodedString); } } |
Output:
Encoded Data: VGVjaGllIERlbGlnaHQ=
Decoded Data: Techie Delight
That’s all about base64 encoding and decoding in Java.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)