package org.ccpit.base.utils;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
 * @Description  �����㷨
 * @Version V0.1
 * @Author zmc
 * @Date 2015��4��3�� ����3:07:18 JDK�汾��sun jdk 1.6
 *       ���¼�¼****************************** �汾�� <�汾��> �޸����ڣ� <����> �޸��ˣ� <�޸�������>
 *       �޸����ݣ� <�޸���������>
 **********************************************************************
 *
 */
public class CryptUtil {

	private static String DES_Algorithm = "DESede";
	private static String MD5_Algorithm = "MD5";
	private static KeyGenerator keygen;
	private static SecretKey deskey;

	public static SecretKey genDESKey(byte[] keyByte) throws Exception {
		SecretKey k = null;
		k = new SecretKeySpec(keyByte, DES_Algorithm);
		return k;
	}

	public static SecretKey genDESKey(String keyStr) throws Exception {
		SecretKey k = null;
		k = new SecretKeySpec(keyStr.getBytes(), DES_Algorithm);
		return k;
	}

	public static SecretKey genDESKey() throws Exception {
		if (keygen == null) {
			keygen = KeyGenerator.getInstance(DES_Algorithm);
			deskey = keygen.generateKey();
		}
		return deskey;
	}

	public static byte[] desEncrypt(SecretKey key, byte[] src) throws Exception {
		Cipher cipher = Cipher.getInstance("DESede");
		cipher.init(1, key);
		return cipher.doFinal(src);
	}

	public static String desEncrypt(SecretKey key, String src) throws Exception {
		return new String(desEncrypt(key, src.getBytes()));
	}

	public static byte[] desEncrypt(byte[] src) throws Exception {
		if (keygen == null) {
			keygen = KeyGenerator.getInstance(DES_Algorithm);
			deskey = keygen.generateKey();
		}
		Cipher cipher = Cipher.getInstance(DES_Algorithm);
		cipher.init(1, deskey);
		return cipher.doFinal(src);
	}

	public static String desEncrypt(String src) throws Exception {
		return new String(desEncrypt(src.getBytes()));
	}

	public static byte[] desDecrypt(SecretKey key, byte[] crypt)
			throws Exception {
		Cipher cipher = Cipher.getInstance(DES_Algorithm);
		cipher.init(2, key);
		return cipher.doFinal(crypt);
	}

	public static String desDecrypt(SecretKey key, String crypt)
			throws Exception {
		return new String(desDecrypt(key, crypt.getBytes()));
	}

	public static byte[] desDecrypt(byte[] crypt) throws Exception {
		if (keygen == null) {
			keygen = KeyGenerator.getInstance(DES_Algorithm);
			deskey = keygen.generateKey();
		}
		Cipher cipher = Cipher.getInstance(DES_Algorithm);
		cipher.init(2, deskey);
		return cipher.doFinal(crypt);
	}

	public static String desDecrypt(String crypt) throws Exception {
		return new String(desDecrypt(crypt.getBytes()));
	}

	public static String md5Encrypt(String src) throws Exception {
		byte[] md5 = md5Digest(src.getBytes("utf-8"));
		return encodeHex(md5);
	}

	private static String md5Digest(String src) throws Exception {
		return new String(md5Digest(src.getBytes("utf-8")));
	}

	private static byte[] md5Digest(byte[] src) throws Exception {
		MessageDigest alg = MessageDigest.getInstance(MD5_Algorithm);
		return alg.digest(src);
	}

	private static byte[] md5Digest(byte[] src, byte[] key) throws Exception {
		MessageDigest alg = MessageDigest.getInstance(MD5_Algorithm);

		alg.update(key);
		return alg.digest(src);
	}

	private static String encodeHex(byte[] bytes) {
		StringBuffer buf = new StringBuffer(bytes.length * 2);

		for (int i = 0; i < bytes.length; i++) {
			if ((bytes[i] & 0xFF) < 16) {
				buf.append("0");
			}
			buf.append(Long.toString(bytes[i] & 0xFF, 16));
		}
		return buf.toString();
	}

	/*public static String base64Encode(String src) {
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(src.getBytes());
	}

	public static String base64Encode(byte[] src) {
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(src);
	}

	public static String base64Decode(String src) {
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			return new String(decoder.decodeBuffer(src));
		} catch (Exception ex) {
		}
		return null;
	}

	public static byte[] base64DecodeToBytes(String src) {
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			return decoder.decodeBuffer(src);
		} catch (Exception ex) {
		}
		return null;
	}*/
}
