方法一
package com.iotck.util;
import java.math.BigInteger;
import java.security.MessageDigest;
public class MD5 {
public static String md5(String plainText){
String encryStr = null;
if (plainText != null && !"".equals(plainText)) {
try {
byte[] ret = MessageDigest.getInstance("md5").digest(plainText.getBytes());
String md5Code = new BigInteger(1, ret).toString(16);
for (int i = 0; i < 32 - md5Code.length(); i++) {
md5Code = "0" + md5Code;
}
encryStr = md5Code;
} catch (Exception e) {
e.printStackTrace();
}
}
return encryStr;
}
}
方法二
package com.iotek.myutil;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Md5 {
public static String md5(String src){
StringBuffer buffer = new StringBuffer();
char[] chars = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f'};
byte[] bytes = src.getBytes();
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] target = md.digest(bytes);
for (byte b : target) {
buffer.append(chars[(b>>4)&0x0F]);
buffer.append(chars[b&0x0F]);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return buffer.toString();
}
}