Hey, Tea Lovers! In this post, we will talk about Base64 in Java. Base64 is a very common and easy way to encode the data. It helps us to at least make it hard to understand the data at first glance to keep it secure. In the Java Base64 library, we have quite unique types of Base64 techniques which you will see in a bit. For this, we will be using the inbuilt Java Base64 Encoder and Decoder. You don’t need any additional Base64 libraries to do this simple task.
For the online Base64 encode and decode tool you can use this very fast and secure tool, here. It is secure because we don’t process your data, encode or decode, on our server. Instead, it’s a completely client-based solution. Do check it out and bookmark it.
You can follow me on social media via @coderstea on Twitter, Linkedin, Facebook, or Instagram. We also share high-quality videos about programming on our Youtube channel. You can also publish your own post on CodersTea.com, just share your thought on Contact Us or let us know in the comments.
Java Base64 Class in Java Util Library
As I said you don’t need any additional library to work on Base64 in Java. You can use Java Base64 via the
util package. The class you have to use is
java.util.Base64. In this Base64 class, we have the static encoder and decoder methods. We will see them one by one shortly.
There are three different types of Base64 encoding and decoding techniques in Java. One is simple or normal Base64, another is for URL and the third one is for MIME friendly data. We will look at them with both encoding and decoding code.
Java Base64 Encoder
Java Base64 encoding is done by static
Base64.Encoder clas. To get this class you have to use static functions from the Base64 class. Use getEncoder() for simple, getUrlEncoder() for URL and getMimeEncoder() for MIME type data. After getting the Encoder, we will have to use encode() function which takes the byte array as a parameter. Don’t worry, we just have to call getBytes() of String you wish as input. These encoding methods will return us the encoded data in a byte array. So we can use encodeToString() method to get the encoded data into the string format. We will see these encoding in a bit.
Java Base64 Decoder
Ok, so you encoded the text, now what. You want to read the original text at some point, right? Then what you have to do is simply decode the Base64 code to its original form. It can be done via
Base64.Decoder class. Similar to
Base64.Encoder class, we have three different types of it. For simple Base64 value use getDecoder(), in case of URL encoded text use getUrlDecoder() and lastly getMimeDecoder() for MIME-encoded data.
Now let us look at the encoding and decoding of various types of data in Java one by one.
Simple Base64 Encoding in Java
To encode a string to base64 in java we have to use the getEncoder() function from the Base64 class. This will give us the Encoder class for simple Base64 encoding. Then we can use encode methods available in the class. All types of Base64 encoder are using the same methods and class, just their types differ. This simple encoding only generates the string containing characters
A-Za-z0-9+/ only. An example is as follows.
// Normal String Encoding String normalText = "This is Normal Text from CodersTea.com"; Base64.Encoder simpleEncoder = Base64.getEncoder(); String encodedText = simpleEncoder.encodeToString(normalText.getBytes()); System.out.println(encodedText); // output: // VGhpcyBpcyBOb3JtYWwgVGV4dCBmcm9tIENvZGVyc1RlYS5jb20=
Simple Base64 Value Decoder in Java
The normal text got converted to base64 value. Now let us reverse the value that brings it to its original form. To do that, we have to use getDecoder() from the Base64 class. As I said the encoder only generates characters
A-Za-z0-9+/ so the decoder will throw an error if it gets any character other than these ones. We will decode the same value we encoded in the last example. But in the decoding case, we will get the byte array instead of String. So we need to manually convert the byte array to String. Don’t worry I will show you how.
// Simple Bse64 decoding Base64.Decoder simpleDecoder = Base64.getDecoder(); /// encodedText: VGhpcyBpcyBOb3JtYWwgVGV4dCBmcm9tIENvZGVyc1RlYS5jb20= byte simpleDecodedByteArray = simpleDecoder.decode(encodedText); String originalSimpleText= new String(simpleDecodedByteArray); System.out.println(originalSimpleText); // output: This is Normal Text from CodersTea.com
URL Encoder in Java
URL is different than the normal text. There are certain conditions for URL text. So this URL Base64 encoded text is safe to use on for both URL and filename. It generates the character containing
A-Za-z0-9+_. only. Let us see the example.
// URL encoder String url = "https://www.coderstea.com/tools/base-64-encode-decode.php"; Base64.Encoder urlEncoder = Base64.getUrlEncoder(); String encodedUrl = urlEncoder.encodeToString(url.getBytes()); System.out.println(encodedUrl); // output: aHR0cHM6Ly93d3cuY29kZXJzdGVhLmNvbS90b29scy9iYXNlLTY0LWVuY29kZS1kZWNvZGUucGhw
As you can see there are no characters that will prevent us to use them in URL or Filename.
URL Decoder in Java
I want to read the original URL. We will use getUrlDecoder() from Base64 to decode the encoded URL value.This decoder will only accept the characters containing A-Za-z0-9+_ otherwise, get ready for some Exceptions. Here is the code.
// URL Decoder Base64.Decoder urlDecoder = Base64.getUrlDecoder(); // encodedUrl = aHR0cHM6Ly93d3cuY29kZXJzdGVhLmNvbS90b29scy9iYXNlLTY0LWVuY29kZS1kZWNvZGUucGhw byte urlDecodedByteArray = urlDecoder.decode(encodedUrl); String originalUrl = new String(urlDecodedByteArray); System.out.println(originalUrl); // output: https://www.coderstea.com/tools/base-64-encode-decode.php
Simple isn’t it. By the way, the URL I showed you is our simple and fast online tool to encode and decode text in Base64. And it’s very secure as we don’t share that data with our server or any server for that matter. So your data is safe and secure with you only.
Base64 Encode MIME data in Java
This MIME encoder, derived by getMimeEncoder() from Base64 class, is used to generate the MIME-friendly text (encoded of course). Where the simple encoder, getEncoder() gives us a long one-liner String no matter how long an input string is, the MIME encoder gives multiline String or byte array output with 76 characters in each line. It uses
\n for carriage return and linefeed respectively. Now let us look at the example.
I have used a long String so that the output would be more than 76 characters and we can see the multiline encoded value.
// MIME encoder String stringForMime = "This post on CodersTea.com is about " + "Base64 Encoding and Decoding techniques in Java " + "without any library"; Base64.Encoder mimeEncoder = Base64.getMimeEncoder(); String encodedTextForMime = mimeEncoder.encodeToString(stringForMime.getBytes()); System.out.println(encodedTextForMime);
The output is as follow for the above example. A multiline MIME friendly string.
Multiline or MIME Base64 Value Decoding in Java
In the above example, we have encoded the text using the MIME base64 encoder, which generates a multiline output contrary to the simple encoder. To decode this multiline or MIME base64 encode value we have to use getMimeDecoder() from the Base64 class. The whole procedure is then the same as other decoders. Here is the example to decode the multiline or MIME base 64 value in Java. we will use the earlier output as an input.
// MIME decoder Base64.Decoder mimeDecoder = Base64.getMimeDecoder(); // encodedTextForMime: // VGhpcyBwb3N0IG9uIENvZGVyc1RlYS5jb20gaXMgYWJvdXQgQmFzZTY0IEVuY29kaW5nIGFuZCBE // ZWNvZGluZyB0ZWNobmlxdWVzIGluIEphdmEgd2l0aG91dCBhbnkgbGlicmFyeQ== byte mimeDecodedByteArray = mimeDecoder.decode(encodedTextForMime); String originalStringForMime = new String(mimeDecodedByteArray); System.out.println(originalStringForMime); // output: This post on CodersTea.com is about Base64 Encoding and Decoding techniques in Java without any library
That’s it for this post. We talked about every possible Base64 encoder and Decoder in the core Java library. We discussed a simple one-liner encoder and decoder, URL encoder and decoder along with MIEM friendly multiline Base64 encoder and decoder in Java. I hope you have liked the post. Please do share your thoughts on it in the comment section and share this post with others.
The full code for all types of Base64 encoding in Java can be found on GitHub.
See you in the next post. HAKUNA MATATA!!!