JDK 自带MD5的使用

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
	public final static String toMD5Str(String str){  //MD5加密算法
		String s = str==null ? "" : str;              //如果为空则返回""
		char hexDigits[] = {'0','1','2','3','4','5','6','7','8',
				'9','A','B','C','D','E','F'};         //字典
		byte[] strTemp = s.getBytes();                //获得二进制
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("MD5"); //加密器
			mdTemp.update(strTemp);                   //执行加密
			byte[] md = mdTemp.digest();                          //加密结果
			int j = md.length;
			char[] value = new char[j*2];             //字符数组
			int k = 0;
			for(int i=0;i<j;i++) {
				byte byte0 = md[i];
				value[k++] = hexDigits[byte0 >>> 4 & 0xf];
				value[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(value);
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}
	
	public static void main(String[] args){
		System.out.println(toMD5Str("你好"));
	}
}

你可能感兴趣的:(JDK自带 MD5)