/**
 * 
 */
package com.emr.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;

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

/**
 * <p>Title:Base64 </p>
 * <p>Description: </p>
 * <p>Company: </p> 
 * @author hu
 * @date
 */
public class Base64 {
	//需加密内容
	private static String src="security base64";
	
	//jdk实现
	public static void jdkBase64(String src){
		try {
			//不建议使用
			BASE64Encoder encoder=new BASE64Encoder();
			String encode=encoder.encode(src.getBytes());
			System.out.println("encode:"+encode);
			
			BASE64Decoder decoder=new BASE64Decoder();		
			System.out.println("decode:"+new String(decoder.decodeBuffer(encode)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//commons Codec实现
	public static void commonsCodecBase64(String src){
		byte[] encodeByts=org.apache.commons.codec.binary.Base64.encodeBase64(src.getBytes());
		System.out.println("encode:"+new String(encodeByts));
		
		byte[] decodeByts=org.apache.commons.codec.binary.Base64.decodeBase64(encodeByts);
		System.out.println("decode:"+new String(decodeByts));
	}
	
	//Bouncy Castle实现
	public static void bouncyCastleBase64(String src){
		byte[] encodeByts=org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
		System.out.println("encode:"+new String(encodeByts));
		
		byte[] decodeByts=org.bouncycastle.util.encoders.Base64.decode(encodeByts);
		System.out.println("decode:"+new String(decodeByts));
	}


	public static String encode(String src) {
		byte[] encodeByts = org.apache.commons.codec.binary.Base64.encodeBase64(src.getBytes());
		return new String(encodeByts);
	}

	public static String decode(String src) {
		byte[] decodeByts = org.apache.commons.codec.binary.Base64.decodeBase64(src.getBytes());
		return new String(decodeByts);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		jdkBase64(src);
		commonsCodecBase64(src);
		bouncyCastleBase64(src);
	}

}
