package com.bcxin.signature.util.common.cipher;

import java.security.MessageDigest;

/**
 * 采用MD5加密解密
 * 
 * @author tfq
 * @datetime 2011-10-13
 */
public class MD5Util {

	/***
	 * MD5加码 生成32位md5码
	 */
	public static String string2MD5(String inStr) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			System.out.println(e.toString());
			e.printStackTrace();
			return "";
		}
		char[] charArray = inStr.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; i++)
			byteArray[i] = (byte) charArray[i];
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16)
				hexValue.append("0");
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();

	}

	/**
	 * 加密解密算法 执行一次加密，两次解密
	 */
	public static String convertMD5(String inStr) {

		char[] a = inStr.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 't');
		}
		String s = new String(a);
		return s;

	}
	
	public static String convertMD52String(String md5Str) {

		return convertMD5(convertMD5(md5Str));

	}

	// 测试主函数
	public static void main(String args[]) {
		//PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/Pgo8cG9saWN5U2FsZVJlcXVlc3Q+CiAgICA8YWRkcmVzc0xpbmVzPuWOpumXqOaAneaYjuWQjuWfrea6qui3rzI45Y+3MTHmpbwxMTEx5Y+3PC9hZGRyZXNzTGluZXM+CiAgICA8YmlydGhEYXRlPjE5ODQtMDgtMDM8L2JpcnRoRGF0ZT4KICAgIDxjb3JlQ29kZT5NWV9HQVNfMDAxPC9jb3JlQ29kZT4KICAgIDxjb3JlTmFtZT7mtYvor5Xnh4PmsJTpmak8L2NvcmVOYW1lPgogICAgPGVtYWlsPjg2OTk2OTBAcXEuY29tPC9lbWFpbD4KICAgIDxleHRlcm5hbFJlZmVyZW5jZT5NWV9HQVNfMDAxPC9leHRlcm5hbFJlZmVyZW5jZT4KICAgIDxmdWxsTmFtZT7otbXpo548L2Z1bGxOYW1lPgogICAgPGdlbmRlcj5NPC9nZW5kZXI+CiAgICA8aWROdW1iZXI+MjEwMTEzMTk4NDA4MDMxNjU2PC9pZE51bWJlcj4KICAgIDxpZFR5cGU+SURDQVJEPC9pZFR5cGU+CiAgICA8aW5zdXJhbmNlQ29tcGFueUNvZGU+QUlHPC9pbnN1cmFuY2VDb21wYW55Q29kZT4KICAgIDxpbnN1cmVkcz4KICAgICAgICA8YWRkcmVzc0xpbmVzPuWOpumXqOaAneaYjuWQjuWfrea6qui3rzI45Y+3MTHmpbwxMTEx5Y+3PC9hZGRyZXNzTGluZXM+CiAgICAgICAgPGJpcnRoRGF0ZT4xOTg0LTA4LTAzPC9iaXJ0aERhdGU+CiAgICAgICAgPGVtYWlsPjg2OTk2OTBAcXEuY29tPC9lbWFpbD4KICAgICAgICA8ZnVsbE5hbWU+6LW16aOePC9mdWxsTmFtZT4KICAgICAgICA8Z2VuZGVyPk08L2dlbmRlcj4KICAgICAgICA8aWROdW1iZXI+MjEwMTEzMTk4NDA4MDMxNjU2PC9pZE51bWJlcj4KICAgICAgICA8aWRUeXBlPklEQ0FSRDwvaWRUeXBlPgogICAgICAgIDxtb2JpbGVOdW1iZXI+MTg2MTE5OTk1MDU8L21vYmlsZU51bWJlcj4KICAgIDwvaW5zdXJlZHM+CiAgICA8bG9naW5OYW1lPmFkbWluPC9sb2dpbk5hbWU+CiAgICA8bW9iaWxlTnVtYmVyPjE4NjExOTk5NTA1PC9tb2JpbGVOdW1iZXI+CiAgICA8cHJvZHVjdFR5cGU+TElGRTwvcHJvZHVjdFR5cGU+CjwvcG9saWN5U2FsZVJlcXVlc3Q+Cg==
		String s = new String("0dd0d2bfcbc06559b82dbb47fcf2ae16");
		//System.out.println("原始：" + s);
		//System.out.println("MD5后：" + string2MD5(s));
		//0dd0d2bfcbc06559b82dbb47fcf2ae16
		//System.out.println("解密==：" + convertMD5(string2MD5(s)));
		//System.out.println("解密的：" + convertMD5(s));
		//System.out.println("2解密的：" + convertMD52String(s));
	}
}
