MD5 加密 参数加密解密

MD5 加密     参数加密解密

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;

public class Md5Utils {

	public static void main(String[] args) {
		// 请求参数加密
		String verifyCode = "transType=C000&custId=custId";
		String queryString = "transType=C000&custId=custId" + "&verifyCode="
				+ getVerifyCode(MD5_SEED, verifyCode);
		System.out.println(queryString);

		// 返回参数加解密
		String enc = encode(verifyCode);
		String dec = decode(enc);
		System.out.println(enc);
		System.out.println(dec);

	}

	private static final String key0 = "aaaaaaa";
	private static final Charset charset = Charset.forName("UTF-8");
	private static byte[] keyBytes = key0.getBytes(charset);

	// 加密
	public static String encode(String enc) {
		byte[] b = enc.getBytes(charset);
		for (int i = 0, size = b.length; i < size; i++) {
			for (byte keyBytes0 : keyBytes) {
				b[i] = (byte) (b[i] ^ keyBytes0);
			}
		}
		return new String(b);
	}

	// 解密
	public static String decode(String dec) {
		byte[] e = dec.getBytes(charset);
		byte[] dee = e;
		for (int i = 0, size = e.length; i < size; i++) {
			for (byte keyBytes0 : keyBytes) {
				e[i] = (byte) (dee[i] ^ keyBytes0);
			}
		}
		return new String(e);
	}

	/***********************************************************************/
	/**
	 * 文忠接口需要的秘钥
	 */
	private static final String MD5_SEED = "bbbbbbbbkeykey";

	private static String getVerifyCode(String md5, String verifyCode) {
		return getMD5Value(md5 + getCurrentDate() + verifyCode);
	}

	private static String getCurrentDate() {
		StringBuffer buffer = new StringBuffer();
		Calendar now = Calendar.getInstance();
		String monthValue = null;
		String dayValue = null;

		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;
		int DAY_OF_MONTH = now.get(Calendar.DAY_OF_MONTH);
		if (month < 10) {
			monthValue = "0" + String.valueOf(month);
		} else {
			monthValue = String.valueOf(month);
		}
		if (DAY_OF_MONTH < 10) {
			dayValue = "0" + String.valueOf(DAY_OF_MONTH);
		} else {
			dayValue = String.valueOf(DAY_OF_MONTH);
		}
		return buffer.append(year).append(monthValue).append(dayValue)
				.toString();
	}

	private static String getMD5Value(String preValue) {
		String resultString = null;
		if (preValue != null && preValue.length() != 0) {
			try {
				MessageDigest md = MessageDigest.getInstance("MD5");
				resultString = byteToString(md.digest(preValue.getBytes()));

			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}

	// 转换字节数组为16进制字串
	private static String byteToString(byte[] bByte) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < bByte.length; i++) {
			sBuffer.append(byteToArrayString(bByte[i]));
		}
		return sBuffer.toString();
	}

	// 返回形式为数字跟字符串
	private static String byteToArrayString(byte bByte) {
		int iRet = bByte;
		if (iRet < 0) {
			iRet += 256;
		}
		int iD1 = iRet / 16;
		int iD2 = iRet % 16;
		return strDigits[iD1] + strDigits[iD2];
	}

	// 全局数组
	private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

}


你可能感兴趣的:(MD5,加密,参数加密解密)