Base64 Encoding and Decoding in JAVA

Base64 Encoding and Decoding in JAVA


Thanks to the original writer: https://itnext.io/multiple-spring-boot-apps-same-project-f6208d1a97d9

We often felt the need for encoding or decoding base64 strings. Often, the access token we use, Kubernetes external secrets, or database passwords are encoded in base64 format. But what exactly is base64?

Base64 is an encoding format which maps the input to a set of characters in the A-Za-z0–9+/ character set.


You might have come across some websites which instantly perform the encoding and decoding for the user.

But is it safe to use an external platform for decoding and encoding your passwords? Definitely not, as the site might be saving your data in the backend. Such encoded data must be decoded locally on your system to avoid any security risk.


How to encode base64 in java?

Base64 encoding and decoding capabilities were added in java8 via the java.util.Base64 utility class.


To encode any string, we can use the encodeToString() method of Encoder class which is an inner class of Base64. In order to encode any string, we need to first convert it into a bytes array. Let’s see how can we encode a simple string like “hello world”.

String value = "hello world";
byte[] bytes = value.getBytes();
Base64.Encoder encoder = Base64.getEncoder();
String encoded = encoder.encodeToString(bytes);
System.out.println(encoded); 
// Output: aGVsbG8gd29ybGQ=        

To decode any string, we can use the decode() method of the Decoder class which is an inner class of Base64. The decode() method returns a byte array instead of a string, that can be converted into a string using the String constructor.

String encoded = "aGVsbG8gd29ybGQ=";
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(encoded);
String decoded = new String(bytes);
System.out.println(decoded);
// Output: hello world        

Base64 URL encoding in java

URL encoding is slightly different from the normal string encoding as it is based on A-Za-z0–9-_ character set.


Let’s see how we can encode a sample URL like www.google.com into base64 using encodeToString method of UrlEncoder inner class inside Base64 class.

String value = "www.google.com";
String encoded = Base64.getUrlEncoder().encodeToString(value.getBytes());
System.out.println(encoded);

// Output: d3d3Lmdvb2dsZS5jb20=        

In the same manner, URL decoding from base64 can be done using the decode method of UrlDecoder inner class inside Base64 class.

String value = "d3d3Lmdvb2dsZS5jb20=";
String decoded = 
new String(Base64.getUrlDecoder().decode(value.getBytes()));
System.out.println(decoded);        
You might be wondering, why there are ‘=’ characters present in the encoded strings. After all, encoder should use A-Za-z0–9-_ character set only. Well, in Base64 encoding, the length of an output-encoded String must be a multiple of three and the encoder adds one or two padding characters (=) at the end of the output as needed in order to meet this requirement.


Conclusion

Encoding and decoding strings are a common practice nowadays in order to keep sensitive data encrypted, and base64 is one of the most widely used encoding formats for the same. Having the flexibility to perform such operations in Java is a great way to store and transfer our sensitive data in a secure format.


"Encoding and decoding strings are a common practice nowadays in order to keep sensitive data encrypted". ----------------- Base64 is not encryption -- it's an encoding. It's a way of representing binary data using only printable (text) characters. https://stackoverflow.com/questions/4070693/what-is-the-purpose-of-base-64-encoding-and-why-it-used-in-http-basic-authentica

Like
Reply

To view or add a comment, sign in

More articles by Omar Ismail

Others also viewed

Explore content categories