Showing posts with label Decoding. Show all posts
Showing posts with label Decoding. Show all posts

Encoding and Decoding

Base64 encoding is an algorithm which uses 64 printable characters to replace each character from original data in an algorithmic sequence, so that as same we can decode these data. In most of the places we need to Encode or even need to Encrypt most sensitive data when we transfer to other network. Encoding is not encryption. In all our below programs we use Base64 to encode a string that scrambles the output and it may appear to be unreadable binary data. But it can be easily decoded if some have knowledge on encoding algorithms. Most often encoding a string will end with "=" symbol. Apart from encoding there are lot of advanced encryption algorithms like MD5, RSA-SHA etc., which are all not easy to break it.
In java we have got function to encode and decode strings. Lets see simple example for encoding and decoding using sun.misc package classes,


public class EncodeDecodeTest {

 public static void main(String[] args) {

  String str = "Java Discover & Questions $$$";
  System.out.println("Input   : "+str);
  try{
   str = new sun.misc.BASE64Encoder().encode(str.getBytes("UTF8"));
   System.out.println("Encoded : "+str);
   
   str = new String((new sun.misc.BASE64Decoder().decodeBuffer(str)), "UTF8");
   System.out.println("Decoded : "+str);
  
  }catch (IOException e) {
   e.printStackTrace();
  }
 }
}


OUTPUT:


Input   : Java Discover & Questions $$$
Encoded : SmF2YSBEaXNjb3ZlciAmIFF1ZXN0aW9ucyAkJCQ=
Decoded : Java Discover & Questions $$$


sun.misc classes are not guaranteed to be consistent across different versions of JRE. In those cases we can use 3rd part open source code or jars to encode our data. There are lot of other mechanism to encode and decode our sensitive data and few are,
  • Using Apache Commons Codec
  • Using Java Mail API (MIME Utility)
  • Using MiGBase64



Using Apache Commons Codec - Link to Download 


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

public class EncodeDecodeTest {

 public static void main(String[] args) {

  String str = "Java Discover & Questions $$$";
  System.out.println("Input   : "+str);
  
  str = new String(Base64.encodeBase64(str.getBytes()));
  System.out.println("Encoded : "+str);

  str = new String(Base64.decodeBase64(str.getBytes()));
  System.out.println("Decoded : "+str);     
 }
}

OUTPUT:


Input  : Java Discover & Questions $$$
Encoded : SmF2YSBEaXNjb3ZlciAmIFF1ZXN0aW9ucyAkJCQ=
Decoded : Java Discover & Questions $$$



Using Java Mail API (MIME Utility) - Link to Download


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;


public class EncodeDecodeTest {

 public static void main(String[] args){

  String str = "Java Discover & Questions $$$";
  System.out.println("Input   : "+str);
  try{
   ByteArrayOutputStream byteAOS = new ByteArrayOutputStream();
   OutputStream outStream = MimeUtility.encode(byteAOS, "base64");
   outStream.write(str.getBytes());
   outStream.close();
   System.out.println("Encoded : " + byteAOS.toString());

   ByteArrayInputStream byteAIS = new ByteArrayInputStream(byteAOS.toByteArray());
   InputStream intStream = MimeUtility.decode(byteAIS, "base64");
   byte[] tmp = new byte[byteAOS.toByteArray().length];
   int length = intStream.read(tmp);
   byte[] decoded = new byte[length];
   System.arraycopy(tmp, 0, decoded, 0, length);
   System.out.println("Decoded : " + new String(decoded));
      
  }catch (MessagingException  e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

OUTPUT:


Input   : Java Discover & Questions $$$
Encoded : SmF2YSBEaXNjb3ZlciAmIFF1ZXN0aW9ucyAkJCQ=

Decoded : Java Discover & Questions $$$



Using MiGBase64 - Link to Download

MiGBase64 is a simple and small java class which is fast and effective encoding and decoding algorithm. 


public class EncodeDecodeTest {

 public static void main(String[] args){

  String str = "Java Discover & Questions $$$";
  System.out.println("Input   : "+str);

  str = Base64.encodeToString(str.getBytes(), true);
  System.out.println("Encoded : " + str);

  str = new String(Base64.decode(str.getBytes()));
  System.out.println("Decoded : " + new String(str));
 }
}

OUTPUT:


Input   : Java Discover & Questions $$$
Encoded : SmF2YSBEaXNjb3ZlciAmIFF1ZXN0aW9ucyAkJCQ=
Decoded : Java Discover & Questions $$$