Java code to encode and decode base64
package pritom; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * Created with IntelliJ IDEA. * User: pritom * Date: 12/12/13 * Time: 10:41 AM * To change this template use File | Settings | File Templates. */ public class EncodeDecodeBase64 { public static void main(String[] args) throws Exception{ String baseString = "TESTING BASE 64 ENCODING AND DECODING WITH JAVA"; System.out.println("Base string: " + baseString); /** * Encoding to base 64 */ BASE64Encoder base64Encoder = new BASE64Encoder(); String encodedString = base64Encoder.encodeBuffer(baseString.getBytes()); System.out.print("Encoded base 64: " + encodedString); /** * Decoding from base 64 */ BASE64Decoder base64Decoder = new BASE64Decoder(); String decodedString = new String(base64Decoder.decodeBuffer(encodedString), "UTF-8"); System.out.println("Decoded base 64: " + decodedString); } }
Output
Base string: TESTING BASE 64 ENCODING AND DECODING WITH JAVA Encoded base 64: VEVTVElORyBCQVNFIDY0IEVOQ09ESU5HIEFORCBERUNPRElORyBXSVRIIEpBVkE= Decoded base 64: TESTING BASE 64 ENCODING AND DECODING WITH JAVA