import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
/**
* 将byte数组转化为16进制输出
*
* @param bytes
* @return
*/
public static String convertByteToHexString(byte[] bytes) {
String result = "";
for (int i = 0; i < bytes.length; i++) {
int temp = bytes[i] & 0xff;
String tempHex = Integer.toHexString(temp);
if (tempHex.length() < 2) {
result += "0" + tempHex;
} else {
result += tempHex;
}
}
return result;
}
/**
* MD5加密
*
* @param message
* @return
* @throws UnsupportedEncodingException
*/
public static String md5Jdk(String message) throws UnsupportedEncodingException {
String temp = "";
try {
MessageDigest md5Digest = MessageDigest.getInstance("MD5");
message.getBytes();
byte[] encodeMD5Digest = md5Digest.digest(message.getBytes("UTF-8"));
temp = convertByteToHexString(encodeMD5Digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return temp;
}
}