Encoding and Decoding with Java Base64 Class

Posted By : Rozi Ali | 20-May-2020

Introduction and Types

With variety of features introduced in Java 8, Base64 class was also added to deal with encryption and decryption. Base64 class provides static methods to encrypt and decrypt data. It supports following types of Base64:

  • Basic : It uses "The Base64 Alphabet" as specified by java in RFC 4648 and RFC 2045 for encoding and decoding operation. If any line feed character is added, the encoder would not accept it and during decoding if decoder find that the data contains characters other than the Base64 alphabets, it will reject the data.
// Encode 
Base64.getEncoder().encodeToString("Encoding using base64".getBytes("utf-8"));
RW5jb2RpbmcgdXNpbmcgYmFzZTY0 // Encoded form

// Decode
Base64.getDecoder().decode("RW5jb2RpbmcgdXNpbmcgYmFzZTY0");
Decoding using base64 // Decoded form

 

  • URL and Filename safe : It uses "URL and Filename safe Base64 Alphabet" as specified by java in RFC 4648 for encoding and decoding operation.  If any line feed character is added, the encoder would not accept it and during decoding if decoder find that the data contains characters other than the Base64 alphabets, it will reject the data.
    No line separator is to be added to the end of the encoded output. All other characters and line separators are ignored by the encoder.
Base64.getEncoder().encodeToString("Encoding?data".getBytes("utf-8"));
// Encoded as: RW5jb2Rpbmc/ZGF0YQ==

Base64.getUrlEncoder().encodeToString("Encoding?data".getBytes("utf-8"));
//Encoded as: RW5jb2Rpbmc_ZGF0YQ==

 

  • MIME : It uses "The Base64 Alphabet" as specified by java in RFC 2045 for encoding and decoding operation. The encoded data must be represented in lines of 76 or less characters and must uses a carriage return 'r' followed by a linefeed 'n' as the line separator.
import java.util.*;

class EncodingUsingMime{
 public static void main(String args[])
 {
  byte[] dataToEncode = null;
  try{
   data = "Example of data encryption".getBytes("utf-8");
  }
  catch(Exception e)
  {}
  System.out.println(Base64.getMimeEncoder().encodeToString(data));
 }
}
// Encoded as: RXhhbXBsZSBvZiBkYXRhIGVuY3J5cHRpb24=

 

Conclusion

With this feature, a java developer no need to use external dependencies to encode or decode. As it is userful for data transmission from binary to ASCII characters, which is a better to transfer binary data via stream. 

About Author

Author Image
Rozi Ali

Rozi Ali is an accomplished software developer with extensive experience in the field of JAVA. She possesses a solid grasp of programming languages such as Java/Spring-boot, Python, and Typescript/Nodejs/GraphQL. Rozi has a strong background in Object-oriented programming (OOP) and is skilled in working with both relational databases like MySql, PostgreSQL and non-relational databases like MongoDb. She is proficient in REST APIs, Microservices, and code deployment, along with the development tools such as Jira, Git, and Bash. Additionally, Rozi has experience working with Cloud providers such as AWS and Azure. She has contributed significantly to a number of projects, including Konfer, VNS, Influsoft, VN Platform, QuickDialog, and Oodles-Dashboard.

Request for Proposal

Name is required

Comment is required

Sending message..