How to encode decode String in Java base64 Encoding? Example Tutorial (original) (raw)
Encoding and Decoding of String in Java using base64 are extremely easy if you are using Apache commons code open source library. it provides convenient static utility methods Base64.encodeBase64(byte[]) and Base64.decodeBase64(byte []) for converting binary data into base64 encoded binary Stream and then decoding back from encoded data in base64 encoding mechanism. Many times we need to encode sensitive data be it binary String format transferring over a socket or transferring or storing data in XML files. Though there are other open-source libraries available that provide support to encode any String into base64 like MimeUtil from javax.mail package but Apache commons-codec package is really handy and doesn't add a lot of extra stuff. Its API is also very neat and clean. In this article, we will see how to encode and decode String into base64 encoding.
Base64 Encoding Algorithm
For those who are not familiar with the base64 encoding algorithm here is the basic introduction. Base64 encodes (changes content of original String) String using an algorithm that uses 64 printable characters to replace each character in an original string in an algorithmic sequence so that it can be decoded later.
Base64 encoding prevents misuse of data by encoding it into ASCII format.
Though there are more advanced encryption algorithms available like RSA-SHA and MD5 and you should use those in production but base64 is real simple and easy to use for simple encoding needs.
Here is how Base64 Encoding works when used :
In this example, we have used Base64.getEncoder().encodeToString() to encode a string to Base64 and Base64.getDecoder().decode() for decoding.
Base64 encoding is commonly used to represent binary data in a text format that can be safely transmitted in contexts where binary data might cause issues (like email or JSON).
The Java 8+ standard library provides the java.util.Base64 class for this purpose, making encoding and decoding operations straightforward.
Encoding and Decoding String into Base64 Java
Here is a quick example of the encoding and decoding of String in the base64 encoding algorithm. we will first encode String by converting it into a byte array and then passing it down to Base64.encodeBase64(byte[]) which encodes byte array as per the base64 encoding algorithm and returns an encoded byte array, which can be converted into String.
Later half, we will decode the String by calling Base64.decodeBase64(byte[]) method.
import org.apache.commons.codec.binary.Base64;
/**
* Java program to encode and decode String in Java using the Base64 encoding algorithm
* @author http://javarevisited.blogspot.com
*/
public class Base64EncodingExample{
public static void main(String args[]) throws IOException {
String orig = "original String before base64 encoding in Java";
//encoding byte array into base 64
byte[] encoded = Base64.encodeBase64(orig.getBytes());
System.out.println("Original String: " + orig );
System.out.println("Base64 Encoded String : " + new String(encoded));
//decoding byte array into base64
byte[] decoded = Base64.decodeBase64(encoded);
System.out.println("Base 64 Decoded String : " + new String(decoded));
}
}
Output:
Original String: original String before base64 encoding in Java
Base64 Encoded String : b3JpZ2luYWwgU3RyaW5nIGJlZm9yZSBiYXNlNjQgZW5jb2RpbmcgaW4gSmF2YQ==
Base 64 Decoded String : original String before base64 encoding in Java
Dependency
In order to execute this Base64 Encoding Example in Java, you need to download and add commons-codec-1.2.jar into your application classpath. if you are using Maven for managing your project dependency you can include the following
dependency in pom.xml:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>20041127.091804</version>
</dependency>
This is a very simple and clean base64 encoding example in Java but serves the purpose. That's all in how to encode Java String into Base64 encoding and how to decode Java String. let me know if you face any issues.
Note - From JDK 8 onwards, you can convert a String into Base64 without using any third party library like commons-codec. Instead you can use Base64.getEncoder().encodeToString() to encode a string to Base64 and Base64.getDecoder().decode() for decoding.
The Base64 class is added from JDK 8 onwards.
Other Java Tutorials you may like: