Encode Decode example using Base64

This example will illustrate how to encode/decode the string value using the Base64 java class which is a part of apache commons package. If you are using maven as a build tool you can add the following dependency into your pom.xml file in order to compile your java class.

      <dependency>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
          <version>1.6</version>
      </dependency>


Java class to encode and decode any String value:

import org.apache.commons.codec.binary.Base64;

public class EncodeDecode {

    public static String base64Encode(String stringToEncode) {
        byte[] stringToEncodeBytes = stringToEncode.getBytes();
        return Base64.encodeBase64String(stringToEncodeBytes);
    }

    public static String base64Decode(String stringToDecode) {
        byte[] decodedBytes = Base64.decodeBase64(stringToDecode);
        return new String(decodedBytes);
    }

    public static void main(String[] args) {
        String testString1 = "Amzi 123";
        // Total bytes=8 divide by 3 and 2
        // bytes are in remainder so '=' is
        // appended in the encoded string
        String testString2 = "Amzi_12345"; 
        // Total bytes=10 divide by 3 and 1
        // bytes are in remainder so '==' is
        // appended in the encoded string
        String testString3 = "Amzi..";
        // Total bytes=6 divide by 3 and 0
        // bytes are in remainder so
        // nothing is appended

        String encoded1 = EncodeDecode.base64Encode(testString1);
        String encoded2 = EncodeDecode.base64Encode(testString2);
        String encoded3 = EncodeDecode.base64Encode(testString3);

        System.out
        .println("******** Encoded Strings *****************");
        System.out.println(testString1 + " > Encoded > " + encoded1);
        System.out.println(testString2 + " > Encoded > " + encoded2);
        System.out.println(testString3 + " > Encoded > " + encoded3);

        System.out
        .println("\n********** Decoded Strings ***************");
        System.out.println(encoded1 + " > Decoded > "
                + EncodeDecode.base64Decode(encoded1));
        System.out.println(encoded2 + " > Decoded > "
                + EncodeDecode.base64Decode(encoded2));
        System.out.println(encoded3 + " > Decoded > "
                + EncodeDecode.base64Decode(encoded3));
    }
}


Output:

******** Encoded Strings *****************
Amzi 123 > Encoded > QW16aSAxMjM=
Amzi_12345 > Encoded > QW16aV8xMjM0NQ==
Amzi.. > Encoded > QW16aS4u

********** Decoded Strings ***************
QW16aSAxMjM= > Decoded > Amzi 123
QW16aV8xMjM0NQ== > Decoded > Amzi_12345
QW16aS4u > Decoded > Amzi..

2 comments:

  1. I'm not a developer, i always use the free online base64 string converter to encode and decode base64.
    y

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here.
    Kindly keep blogging. If anyone wants to become a Java developer learn from Java EE Online Training from India.
    or learn thru Java EE Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete