Table Of Content

Encode and decode example in Java using Apache Base64

View more categories:

1- Encode and Decode Example

An example using Apache Axis library to encode and decode a text:
Apache Axis Library, you can download at:
If you use Maven:
* pom.xml *
package org.o7planning.example.encode;


import org.apache.axis.encoding.Base64;

public class EncodeDecodeExample {

   // Encode
   public static String encodeString(String text)
           throws UnsupportedEncodingException {
       byte[] bytes = text.getBytes("UTF-8");
       String encodeString = Base64.encode(bytes);
       return encodeString;

   // Decode
   public static String decodeString(String encodeText)
           throws UnsupportedEncodingException {
       byte[] decodeBytes = Base64.decode(encodeText);
       String str = new String(decodeBytes, "UTF-8");
       return str;
   public static void main(String[] args) throws UnsupportedEncodingException  {
       String text = "Example Vietnamese text - Tiếng Việt";
       System.out.println("Text before encode: "+ text);
       String encodeText = encodeString(text);
       System.out.println("Encode text: "+ encodeText);
       String decodeText =  decodeString(encodeText);
       System.out.println("Decode text: "+ decodeText);        

Results of running the example:

View more categories: