/**
 * 
 */
package com.manage.encrypt;
import java.io.IOException;

//import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 * <p>Title:Base64 </p>
 * <p>Description: </p>
 * <p>Company: </p> 
 * @author hu
 * @date
 */
public class Base64 {
	//需加密内容
	private static String str="abc123%.";

	//jdk实现
	public static void jdkBase64(String src){
		try {
			//不建议使用
			BASE64Encoder encoder=new BASE64Encoder();
			String encode=encoder.encode((str+src+str).getBytes());
			System.out.println("encode:"+encode);
			
			BASE64Decoder decoder=new BASE64Decoder();		
			System.out.println("decode:"+new String(decoder.decodeBuffer(encode)).replace(str,""));
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	//base64编码
	public static String encodeBase64(String src){
		try {
			//不建议使用
			BASE64Encoder encoder=new BASE64Encoder();
			String encode=encoder.encode((str+src+str).getBytes());
			return encode;
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return null;
		}
	}
	//base64解码
	public static String decodeBase64(String encode){
		try {
			BASE64Decoder decoder=new BASE64Decoder();
			return new String(decoder.decodeBuffer(encode)).replace(str,"");
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return null;
		}
	}
	//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) {
		String src = "123456";
		String kl = MD5.KL(src);
		System.out.println(kl);
		String encode = encode(kl);
		System.out.println(encode);
		encode = "REdGRkNEQ0U=";
		String s = decode(encode);
		System.out.println(s);
		String jm = MD5.JM(s);
		System.out.println(jm);
		//commonsCodecBase64(src);
		//bouncyCastleBase64(src);
	}

}
