Java 8 Base64 Encoding and Decoding Example


In this guide, we’ll explore the Java 8 Base64 utility class for obtaining encoders and decoders for the Base64 encoding scheme. Base64 is a utility class under the package java.util since 1.8.

In Java 8, we can use three types of Base64 encoding:

  1. Basic or Simple
  2. URL and Filename
  3. MIME

The implementation of the Base64 class supports the following types of Base64 as specified in RFC 4648 and RFC 2045.

1. Java 8- Basic or Simple

The encoded string is mapped to a set of characters lying in A-Za-z0-9+/. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the A-Za-z0-9+/.

1.1 Basic Base64 Encoding

String originalString = "Stay Home Stay Safe";
System.out.println("Original String: " + originalString);


String encodedString = Base64.getEncoder().encodeToString(originalString.getBytes());
System.out.println("Encoded String: " + encodedString);

1.2 Basic Base64 Decoding

byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
String decodedString = new String(decodedBytes);
System.out.println("Decoded String: " + decodedString);

Output

Original String: Stay Home Stay Safe
Encoded String: U3RheSBIb21lIFN0YXkgU2FmZQ==
Decoded String: Stay Home Stay Safe

2. Java 8- URL and Filename

The encoded string is mapped to a set of characters lying in A-Za-z0-9+_. The decoder rejects data that contains characters outside the A-Za-z0-9+_.

2.1 URL Encoding

String originalURL = "https://www.google.com/search?ei=KIyJXtq4KOic4-EP_P-10A0&q=websparrow.org";
System.out.println("Original URL String: " + originalURL);


String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes());
System.out.println("Encoded URL String: " + encodedURL);

2.2 URL Decoding

byte[] decodedURLBytes = Base64.getUrlDecoder().decode(encodedURL);
String decodedURL = new String(decodedURLBytes);
System.out.println("Decoded URL String: " + decodedURL);

Output

Original URL String: https://www.google.com/search?ei=KIyJXtq4KOic4-EP_P-10A0&q=websparrow.org
Encoded URL String: aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9zZWFyY2g_ZWk9S0l5Slh0cTRLT2ljNC1FUF9QLTEwQTAmcT13ZWJzcGFycm93Lm9yZw==
Decoded URL String: https://www.google.com/search?ei=KIyJXtq4KOic4-EP_P-10A0&q=websparrow.org

3. Java 8- MIME

The encoded output must be represented in lines of no more than 76 characters each and uses a carriage return ‘\r’ followed immediately by a linefeed ‘\n’ as the line separator. No line separator is present to the end of the encoded output.

3.1 MIME Encoding

StringBuilder originalMimeString = new StringBuilder();
for (int ctr = 0; ctr < 10; ctr++) {
	originalMimeString.append(UUID.randomUUID().toString());
}

System.out.println("Original MIME String: " + originalMimeString);


String encodedMime = Base64.getMimeEncoder()
		.encodeToString(originalMimeString.toString().getBytes());
System.out.println("Encoded MIME String: " + encodedMime);

3.2 MIME Decoding

byte[] decodedMimeBytes = Base64.getMimeDecoder().decode(encodedMime);
String decodedMime = new String(decodedMimeBytes);
System.out.println("Decoded MIME String: " + decodedMime);

Output

Original MIME String: 46b1778d-d8be-4cb6-9b80-8d439bfd443321d9e6a1-aae2-482e-81e5-742f85e5a1187694bd39-c051-42aa-a990-ae638d8b5bc2fc07cfae-1b82-412f-ba0a-1b7afa88e7882ec0bd15-4d70-45b3-83dc-7ae0b97856c3f2c7400e-b447-4630-bb14-622174091b9fe4e6fdbf-e218-4dcc-8d8b-c65f0f6079584a9eb92e-436e-444b-80f3-89583b96524ccaa48e6c-94c9-4d9d-9b40-676fc488d01d3560a4c4-9e9e-4a0c-a121-d986df398e11
Encoded MIME String: NDZiMTc3OGQtZDhiZS00Y2I2LTliODAtOGQ0MzliZmQ0NDMzMjFkOWU2YTEtYWFlMi00ODJlLTgx
ZTUtNzQyZjg1ZTVhMTE4NzY5NGJkMzktYzA1MS00MmFhLWE5OTAtYWU2MzhkOGI1YmMyZmMwN2Nm
YWUtMWI4Mi00MTJmLWJhMGEtMWI3YWZhODhlNzg4MmVjMGJkMTUtNGQ3MC00NWIzLTgzZGMtN2Fl
MGI5Nzg1NmMzZjJjNzQwMGUtYjQ0Ny00NjMwLWJiMTQtNjIyMTc0MDkxYjlmZTRlNmZkYmYtZTIx
OC00ZGNjLThkOGItYzY1ZjBmNjA3OTU4NGE5ZWI5MmUtNDM2ZS00NDRiLTgwZjMtODk1ODNiOTY1
MjRjY2FhNDhlNmMtOTRjOS00ZDlkLTliNDAtNjc2ZmM0ODhkMDFkMzU2MGE0YzQtOWU5ZS00YTBj
LWExMjEtZDk4NmRmMzk4ZTEx
Decoded MIME String: 46b1778d-d8be-4cb6-9b80-8d439bfd443321d9e6a1-aae2-482e-81e5-742f85e5a1187694bd39-c051-42aa-a990-ae638d8b5bc2fc07cfae-1b82-412f-ba0a-1b7afa88e7882ec0bd15-4d70-45b3-83dc-7ae0b97856c3f2c7400e-b447-4630-bb14-622174091b9fe4e6fdbf-e218-4dcc-8d8b-c65f0f6079584a9eb92e-436e-444b-80f3-89583b96524ccaa48e6c-94c9-4d9d-9b40-676fc488d01d3560a4c4-9e9e-4a0c-a121-d986df398e11

See the complete example.

Base64EncoderDecoder.java
package org.websparrow;

import java.util.Base64;
import java.util.UUID;

public class Base64EncoderDecoder {

	public static void main(String[] args) {

		System.out.println("============================");
		System.out.println("Basic Encoding and Decoding");
		System.out.println("============================");

		// Basic/ Simple Types
		String originalString = "Stay Home Stay Safe";
		System.out.println("Original String: " + originalString);

		// Basic Encoding
		String encodedString = Base64.getEncoder().encodeToString(originalString.getBytes());
		System.out.println("Encoded String: " + encodedString);

		// Basic Decoding
		byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
		String decodedString = new String(decodedBytes);
		System.out.println("Decoded String: " + decodedString);

		System.out.println("\n==================================");
		System.out.println("URL/Filename Encoding and Decoding");
		System.out.println("==================================");

		// URL or Filename types
		String originalURL = "https://www.google.com/search?ei=KIyJXtq4KOic4-EP_P-10A0&q=websparrow.org";
		System.out.println("Original URL String: " + originalURL);

		// URL/Filename Encoding
		String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes());
		System.out.println("Encoded URL String: " + encodedURL);

		// URL/Filename Decoding
		byte[] decodedURLBytes = Base64.getUrlDecoder().decode(encodedURL);
		String decodedURL = new String(decodedURLBytes);
		System.out.println("Decoded URL String: " + decodedURL);

		System.out.println("\n==========================");
		System.out.println("MIME Encoding and Decoding");
		System.out.println("==========================");

		// MIME types
		StringBuilder originalMimeString = new StringBuilder();
		for (int ctr = 0; ctr < 10; ctr++) {
			originalMimeString.append(UUID.randomUUID().toString());
		}

		System.out.println("Original MIME String: " + originalMimeString);

		// MIME Encoding
		String encodedMime = Base64.getMimeEncoder()
				.encodeToString(originalMimeString.toString().getBytes());
		System.out.println("Encoded MIME String: " + encodedMime);

		// MIME Decoding
		byte[] decodedMimeBytes = Base64.getMimeDecoder().decode(encodedMime);
		String decodedMime = new String(decodedMimeBytes);
		System.out.println("Decoded MIME String: " + decodedMime);

	}
}

References

  1. Class Base64- Java 8
  2. Base64- Wikipedia

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.