方法一:
public static String getMD5(String inputString) {
if(inputString == null) {
return null;
} else {
byte[] source = inputString.getBytes();
String s = null;
char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest e = MessageDigest.getInstance("MD5");
e.update(source);
byte[] tmp = e.digest();
char[] str = new char[32];
int k = 0;
for(int i = 0; i < 16; ++i) {
byte byte0 = tmp[i];
str[k++] = hexDigits[byte0 >>> 4 & 15];
str[k++] = hexDigits[byte0 & 15];
}
s = new String(str);
} catch (Exception var10) {
var10.printStackTrace();
}
return s;
}
}
方法二:
/***
* md5加密
*
* @param content 加密内容
* @return 加密后的字符串
*/
public static String MD5(String content) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(content.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
result = buf.toString();
} catch (NoSuchAlgorithmException e) {
System.out.println(e);
}
return result;
}