by BehindJava

What is Encoding and Decoding in Java

Home » java » What is Encoding and Decoding in Java

In this tutorial we are going to learn about Encoding and Decoding in Java.

In computers, encoding is the process of putting a sequence of characters (letters, numbers, punctuation, and certain symbols) into a specialized format for efficient transmission or storage. Decoding is the opposite process — the conversion of an encoded format back into the original sequence of characters. Encoding and decoding are used in data communications, networking, and storage. The term is especially applicable to radio (wireless) communications systems.

Encoding and Decoding of String in Java using base64 is extremely easy if you are using Apache commons code open source library. it provides convenient static utility method Base64.encodeBase64(byte[]) and Base64.decodeBase64(byte []) for converting binary data into base64 encoded binary Stream and than decoding back from encoded data in base64 encoding mechanism. Many times we need to encode sensitive data be it binary String format transferring over socket or transferring or storing data in xml files. Though there are other open source library available which provides support to encode any String into base64 e.g. MimeUtil from javax.mail package but Apache commons codec package is really handy and doesn’t add 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.

For those who are not familiar with base64 encoding algorithm here is basic introduction. Base64 encodes (changes content of original String) String using an algorithm which uses 64 printable characters to replace each character in 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 algorithm 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 a quick example of encoding and decoding of String in base64 encoding algorithm. we will first encode String by converting it into byte array and than passing down to Base64.encodeBase64(byte[]) which encodes byte array as per base64 encoding algorithm and returns and encoded byte array, which can be converted into String. in Later half we will decode the String by calling Base64.decodeBase64(byte[]) method.

Example:

import org.apache.commons.codec.binary.Base64;
 
/**
 * Java program to encode and decode String in Java using 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