天天看點

MD5Util 工具類

package com.cc.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
	static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	static final char hexDigitsLower[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
			'f' };

	/**
	 * 對字元串 MD5 無鹽值加密
	 * 
	 * @param plainText 傳入要加密的字元串
	 * @return MD5加密後生成32位(小寫字母+數字)字元串
	 */
	public static String MD5Lower(String plainText) {
		try {
			// 獲得MD5摘要算法的 MessageDigest 對象
			MessageDigest md = MessageDigest.getInstance("MD5");

			// 使用指定的位元組更新摘要
			md.update(plainText.getBytes());

			// digest()最後确定傳回md5 hash值,傳回值為8位字元串。因為md5 hash值是16位的hex值,實際上就是8位的字元
			// BigInteger函數則将8位的字元串轉換成16位hex值,用字元串來表示;得到字元串形式的hash值。1 固定值
			return new BigInteger(1, md.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 對字元串 MD5 加密
	 * 
	 * @param plainText 傳入要加密的字元串
	 * @return MD5加密後生成32位(大寫字母+數字)字元串
	 */
	public static String MD5Upper(String plainText) {
		try {
			// 獲得MD5摘要算法的 MessageDigest 對象
			MessageDigest md = MessageDigest.getInstance("MD5");

			// 使用指定的位元組更新摘要
			md.update(plainText.getBytes());

			// 獲得密文
			byte[] mdResult = md.digest();
			// 把密文轉換成十六進制的字元串形式
			int j = mdResult.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = mdResult[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];// 取位元組中高 4 位的數字轉換, >>> 為邏輯右移,将符号位一起右移
				str[k++] = hexDigits[byte0 & 0xf]; // 取位元組中低 4 位的數字轉換
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 對字元串 MD5 加鹽值加密
	 * 
	 * @param plainText 傳入要加密的字元串
	 * @param saltValue 傳入要加的鹽值
	 * @return MD5加密後生成32位(小寫字母+數字)字元串
	 */
	public static String MD5Lower(String plainText, String saltValue) {
		try {
			// 獲得MD5摘要算法的 MessageDigest 對象
			MessageDigest md = MessageDigest.getInstance("MD5");

			// 使用指定的位元組更新摘要
			md.update(plainText.getBytes());
			md.update(saltValue.getBytes());

			// digest()最後确定傳回md5 hash值,傳回值為8位字元串。因為md5 hash值是16位的hex值,實際上就是8位的字元
			// BigInteger函數則将8位的字元串轉換成16位hex值,用字元串來表示;得到字元串形式的hash值。1 固定值
			return new BigInteger(1, md.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 對字元串 MD5 加鹽值加密
	 * 
	 * @param plainText 傳入要加密的字元串
	 * @param saltValue 傳入要加的鹽值
	 * @return MD5加密後生成32位(大寫字母+數字)字元串
	 */
	public static String MD5Upper(String plainText, String saltValue) {
		try {
			// 獲得MD5摘要算法的 MessageDigest 對象
			MessageDigest md = MessageDigest.getInstance("MD5");

			// 使用指定的位元組更新摘要
			md.update(plainText.getBytes());
			md.update(saltValue.getBytes());

			// 獲得密文
			byte[] mdResult = md.digest();
			// 把密文轉換成十六進制的字元串形式
			int j = mdResult.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = mdResult[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * MD5加密後生成32位(小寫字母+數字)字元串 同 MD5Lower() 一樣
	 */
	public final static String MD5(String plainText) {
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");

			mdTemp.update(plainText.getBytes("UTF-8"));

			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigitsLower[byte0 >>> 4 & 0xf];
				str[k++] = hexDigitsLower[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 校驗MD5碼
	 * 
	 * @param text 要校驗的字元串
	 * @param md5  md5值
	 * @return 校驗結果
	 */
	public static boolean valid(String text, String md5) {
		return md5.equals(MD5(text)) || md5.equals(MD5(text).toUpperCase());
	}

	/**
	 * 測試
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		String plainText = "123456";

		System.out.println(MD5(plainText));

	}

}

           
md5