Introduction
In this lab, we will learn how to encode and decode Base64 data in Java using the java.util.Base64
class and the Apache Commons library.
In this lab, we will learn how to encode and decode Base64 data in Java using the java.util.Base64
class and the Apache Commons library.
In this step, you will create a new Java file. Open the terminal and execute the following command to create a new Java file:
touch ~/project/Base64Encoding.java
This command will create a new file named Base64Encoding.java
inside the ~/project
directory. You can use any preferred name, but ensure it ends with .java
.
In this step, we will learn about Basic Encoding and Decoding.
getEncoder()
method that returns a simple Base64.Encoder.encodeToString()
method to encode the string.For decoding an encoded string, we will use the Base64.Decoder returned by the getDecoder()
method.
decode()
method of the decoder.Use the code block below for encoding and decoding a string:
import java.util.Base64;
public class Base64Encoding {
public static void main(String[] args) {
String strToEncode = "Hello World";
byte[] bytesToEncode = strToEncode.getBytes(); // convert the string to a byte array
// Encoding
String encodedStr = Base64.getEncoder().encodeToString(bytesToEncode);
System.out.println("Encoded String: " + encodedStr);
// Decoding
byte[] decodedByteArr = Base64.getDecoder().decode(encodedStr);
String decodedStr = new String(decodedByteArr);
System.out.println("Decoded String: " + decodedStr);
}
}
Execute the following command in the terminal to compile and run the code:
javac Base64Encoding.java && java Base64Encoding
In the previous step, we saw that encoding adds padding characters ( = ) at the end of the encoded string. In this step, we will learn how to encode without padding characters.
Use the following code block for encoding the string without padding:
import java.util.Base64;
public class Base64Encoding {
public static void main(String[] args) {
String strToEncode = "Java";
byte[] bytesToEncode = strToEncode.getBytes(); // convert the string to a byte array
// Encoding without padding
String encodedStr = Base64.getEncoder().withoutPadding().encodeToString(bytesToEncode);
System.out.println("Encoded String: " + encodedStr);
}
}
Execute the following command in the terminal to compile and run the code:
javac Base64Encoding.java && java Base64Encoding
In this step, we will learn about URL and Filename Safe Encoding and Decoding.
The Base64 class handles URL encoding and decoding by using the URL and Filename safe Base64 Alphabet.
getUrlEncoder()
method to obtain a Base64 URL encoder.encodeToString()
method as we did in the previous section.Similarly, we have a getUrlDecoder()
method that returns a URL decoder.
decode()
method with this decoder for decoding.Use the code block below to encode and decode a URL:
import java.util.Base64;
public class Base64Encoding {
public static void main(String[] args) {
String urlToEncode = "https://go.java/?intcmp=gojava-banner-java-com";
// Encoding
String encodedUrl = Base64.getUrlEncoder().encodeToString(urlToEncode.getBytes());
System.out.println("Encoded URL: " + encodedUrl);
// Decoding
byte[] decodedUrlBytes = Base64.getUrlDecoder().decode(encodedUrl);
String decodedUrl = new String(decodedUrlBytes);
System.out.print("Decoded URL: " + decodedUrl);
}
}
Execute the following command in the terminal to compile and run the code:
javac Base64Encoding.java && java Base64Encoding
In this step, we will learn about MIME Encoding and Decoding.
We can use the getMimeEncoder()
and the encodeToString()
methods for the encoding.
To decode, we will use the getMimeDecoder()
method to obtain a decoder, and then we will use the decode()
method.
Use the following code block for encoding and decoding MIME data:
import java.util.Base64;
import java.util.UUID;
public class Base64Encoding {
public static void main(String[] args) {
// Creating a MIME input for encoding
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; ++i) {
sb.append(UUID.randomUUID().toString());
}
// Encoding
byte[] bytes = sb.toString().getBytes();
String mimeEncodedStr = Base64.getMimeEncoder().encodeToString(bytes);
System.out.println("Encoded String: " + mimeEncodedStr);
// Decoding
byte[] mimeDecodedBytes = Base64.getMimeDecoder().decode(mimeEncodedStr);
String mimeDecodedStr = new String(mimeDecodedBytes);
}
}
Execute the following command in the terminal to compile and run the code:
javac Base64Encoding.java && java Base64Encoding
In this step, we will learn how to use the Apache Commons library for encoding and decoding Base64 data.
The Apache Commons library provides a Base64 class inside its org.apache.commons.codec.binary
package.
encode()
and decode()
methods on the string bytes to perform encoding and decoding.Use the following code block to encode and decode a string using the Apache Commons Base64 class:
import org.apache.commons.codec.binary.Base64;
public class Base64Encoding {
public static void main(String[] args) {
Base64 base64 = new Base64();
// Encoding
String strToEncode = "Hello World";
byte[] encodedBytes = base64.encode(strToEncode.getBytes());
String encodedStr = new String(encodedBytes);
System.out.println("Encoded String: " + encodedStr);
// Decoding
byte[] decodedByteArr = base64.decode(encodedStr.getBytes());
String decodedStr = new String(decodedByteArr);
System.out.println("Decoded String: " + decodedStr);
// Directly access the API by using the static methods encodeBase64() and decodeBase64()
String strToEncode2 = "Hello World";
String encodedStr2 = new String(Base64.encodeBase64(strToEncode.getBytes()));
String decodedStr2 = new String(Base64.decodeBase64(encodedStr.getBytes()));
}
}
Execute the following command in the terminal to compile and run the code:
javac -cp commons-codec-1.15.jar Base64Encoding.java && java -cp commons-codec-1.15.jar:. Base64Encoding
Note: To run this code, you need to download and import the Apache Commons Codec library's .jar
file into the classpath. In the command above, we added -cp commons-codec-1.15.jar
to specify the classpath of the library.
In this lab, we learned how to encode and decode Base64 data in Java using the java.util.Base64
class and the Apache Commons library. We covered three different Base64 encoding and decoding schemes - Basic, URL, and Filename safe, and MIME.