Java

How to Do Base64 Encoding and Decoding in Java

Base64 is a well-known coding scheme used in computer programming. This coding scheme represents the binary data in 24 bits that can easily be mapped into four 64-bit Base64 digits. Almost all the programming languages provide us with different ways of using this coding scheme effectively. The basic purpose of using any coding scheme is to encode the given data for the sake of securing it and then decode it as needed. Therefore, we can say that encoding and decoding are the two main processes that are involved while working with any coding scheme. In this tutorial, we want to explore the Base64 encoding and decoding schemes using the Java programming language in Ubuntu 20.04.

Performing Base64 Encoding and Decoding in Java in Ubuntu 20.04

We have used a Ubuntu 20.04 system for demonstrating the process of performing Base64 encoding and decoding in Java. To do that, we simply created an empty document within the Home directory of our system with the title EncodeDecode.java. You can name this file according to your choice followed by the “.java” extension which is mandatory for a Java file. After that, we opened this file and wrote the code shown in the following image:

In this Java code, we have first imported the library “java.util.Base64” that will help us in using the built-in “getEncoder()” and “getDecoder()” functions implemented within this library. After that, we created a public class named “EncodeDecode”. You need to be careful while naming this class because this name should be exactly the same as the name of your Java file. Otherwise, you will not be able to compile your code successfully.

Then, within this class, we have created our “main()” function with the statement “public static void main(String[] args)”. Inside this “main()” function, we have first created a random string “str” that we first want to encode (i.e. I Am A Tech Geek.) Here, you can create any string of your choice. After that, we have simply printed this original string on the terminal so that once this string is encoded and then decoded, you can easily compare the results with the original string.

Then, we have created an instance named “encoder” of the “Base64.Encoder” class and have equalized it to the result of the “Base64.getEncoder()” function. After that, we have created a string named “encodedString” that will be used to hold the encoded string returned as a result of executing the statement “encoder.encodeToString(str.getBytes())”. Then, we have simply printed the encoded string on the terminal.

After that, we wanted to decode this encoded string. To do so, we created an instance “decoder” of the “Base64.Decoder” class and have equalized it to the result of the “Base64.getDecoder()” function. Then, we have created another string named “decodedString” that will be used to store the result of the statement “new String(decoder.decode(encodedString))”. Finally, we have printed the decoded string on the terminal as well.

Confirming the Presence of Java JDK on Ubuntu 20.04

We all know that for running a Java code in Ubuntu 20.04, we must have the Java development kit present on our system. The presence of the JDK can be ensured by running the command shown below:

$ java --version

If the JDK is already there on your Ubuntu 20.04 system, then an output similar to the one shown in the following image will appear on your terminal. After seeing it, you can easily proceed with the compilation of your newly created Java code.

However, if you do not have the JDK installed beforehand, then, you will have to install any of its latest versions first. Only then, you will be able to proceed with this method.

Compiling the Java Code

To compile this Java code, we will use the command shown below:

$ javac EncodeDecode.java

You will have to replace “EncodeDecode” with the name of your particular Java code file or the name of your Java class.

Executing the Java Code

If there are no errors as a result of compiling your Java code, then you can simply execute it with the following command:

$ java EncodeDecode

Here, you just need to use the name of your particular Java class or Java code file without the “.java” extension.

Results of the Java Code

The output that was produced as a result of executing this Java code is shown in the image below.

You can clearly see from the image shown above that upon execution, the first statement that was printed in our original string (i.e. I Am A Tech Geek.) After that, an encoded string is printed which is a combination of some upper-case and lower-case letters, numbers, and special characters. If the decoded string is printed on the terminal is exactly the same as our original string it means that the processes of the Base64 encoding and decoding have been carried out successfully in the Java programming language in Ubuntu 20.04.

Conclusion

Through this article, we wished to teach you the methods of performing the Base64 encoding and decoding in Java in Ubuntu 20.04. We first showed you the Java code that we have used to carry out these processes. After that, we shared a pre-requisite for running a Java code in Ubuntu 20.04 (i.e. the existence of the JDK on your system.) Then, we shared the commands for compiling and executing Java code in Ubuntu 20.04. After that, we shared the results of this Java code with you thoroughly. These results confirmed that we have correctly encoded and decoded our desired string with Base64 in Java in Ubuntu 20.04. Apart from this, you can also use any other compiler such as Visual Studio or any other operating system for running the Java code.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.