Java

How to Perform base64 Encoding and Decoding in Java

While dealing with confidential data in Java, there can be instances where the developer needs to encode or decode the data to maintain its secrecy or utilize it, respectively. For instance, refraining from data transfer without appropriate permissions or utilizing the encoded data. In such situations, “encoding” and “decoding” in Java assist the developer in ensuring data integrity.

This article will elaborate on performing the “base64 encoding” and “decoding” in Java.

How to Perform “base64 Encoding and Decoding” Using Java?

The “base64 encoding” can be performed by using the combined “getEncoder()” and “encodeToString()” methods. The decoding, on the other hand, can be applied by utilizing the combination of the “getDecoder()” and “decode()” methods.

Encoding Methods in Java

The “getEncoder()” method fetches the encoder and the “encodeToString()” method generates the encoded string corresponding to the passed string in the form of bytes.

Decoding Methods in Java

The “getDecoder()” method gets the decoder and the “decode()” method returns the decoded values of the passed encoded string.

Before heading to the examples, make sure to add the “Base64” class in the “java.util package” to enable encoding and decoding:

import java.util.Base64;

Example 1: Perform base64 Encoding and Decoding Upon a String in Java

In this example, the discussed methods can be applied to encode and decode the string using “Base64”:

String givenString = "Linuxhint";
String encode = Base64.getEncoder().encodeToString(givenString.getBytes());
System.out.println("The Encoded String of " + givenString + " in Base 64 is: "
+ encode);
byte[] decodeByte = Base64.getDecoder().decode(encode);
String decode = new String(decodeByte);
System.out.println("The Decoded String of " + encode + " is: "
+ decode);

In the above code, apply the following steps:

  • Firstly, initialize the string that needs to be encoded and decoded.
  • In the next step, pass the string as the “encodeToString()” method parameter and associate the “getBytes()” method with the string.
  • This will encode the passed string in the form of bytes.
  • Also, link the “getEncoder()” method to fetch the encoder.
  • After that, pass the encoded string as a parameter to the “decode()” method, get the decoder via the “getDecoder()” method, and store the result in a bytes array.
  • Now, create a “String” object using the “new” keyword and the “String()” constructor, respectively, and pass the bytes array as its parameter.
  • This will result in returning the decoded value in the form of a string.

Output

In the above output, it can be observed that upon decoding the string, the original string is retrieved again.

Example 2: Perform base64 Encoding (With and Without Padding) and Decoding Upon URL in Java

In this particular example, the “URL” can be encoded and decoded via the “getUrlEncoder()” and “getUrlDecoder()” methods. The former method fetches the URL encoder and the latter method gets the URL decoder.

The added “withoutPadding()” method can be applied to encode the URL without padding. The need for this method is that if the encoded string’s length is not a multiple of “3”, then the “=” character is placed to make the string length a multiple of “3” that can be omitted via this method, as follows:

String givenUrl = "https://linuxhint.com/super-keyword-in-java-2/";
String encode = Base64.getUrlEncoder().encodeToString(givenUrl.getBytes());
String encodewithoutpadd =
Base64.getUrlEncoder().withoutPadding().encodeToString(givenUrl.getBytes());
System.out.println("Encoded URL in Base 64 is: " + encode);
System.out.println("Encoded URL without padding: "
+ encodewithoutpadd);
byte[] decodeByte = Base64.getUrlDecoder().decode(encode);
String decode = new String(decodeByte);
System.out.println("The Decoded URL is: " + decode);

According to the above code block, perform the below-provided steps:

  • Initialize the URL that needs to be encoded (with and without padding) and decoded.
  • In the next step, apply the discussed methods with the replaced “getURLEncoder()” method to fetch the extracted encoder against the URL.
  • After that, include an additional “withoutPadding()” method to encode the URL without padding and display both of the encoded string values.
  • Now, repeat the discussed methodology to decode the URL using the combined “getURLDecoder()” and “decode()” methods and display the resultant decoded value in the form of a string.

Output

In this outcome, it can be analyzed that the “==” is placed in the former encoded string since it is not a multiple of three. In the latter encoding, it is omitted via the “withoutPadding()” method.

Conclusion

To perform base64 Encoding and Decoding in Java, apply the combined “getEncoder()” and “encodeToString()” methods or the “getDecoder()” and “decode()” methods. These methods encode and decode the passed string. The “getUrlEncoder()” and “getUrlDecoder()” methods encode (with or without padding) and decode the URL. This blog is guided to apply base64 encoding and decoding in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.