Wednesday, 22 May 2013

BASE64 encoder decoder in java


Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.


Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remains intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML.

import java.io.IOException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class EncodeDecode {
    
    public static void main(String[] args) throws IOException {
    
        BASE64Encoder  encoder=new BASE64Encoder();
        BASE64Decoder  decoder=new BASE64Decoder();
        
        String encodePassword=encoder.encode("Arun".getBytes());
        System.out.println("Encode Password Arun To "+encodePassword);
        
        byte[] decodePassword=decoder.decodeBuffer(encodePassword);
        System.out.println("Decode Password "+encodePassword+" to "+new String (decodePassword));
        
    }
    
}


OutPut.........
Encode Password Arun To QXJ1bg==
Decode Password QXJ1bg== to Arun