import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
public static String MD5(String inStr) throws NoSuchAlgorithmException {
MessageDigest md5 = null;
md5 = MessageDigest.getInstance("MD5");
byte[] byteArray = inStr.getBytes();
byte[] md5Bytes = md5.digest(byteArray);
String str=byte2hex(md5Bytes);
return str;
}
private static String byte2hex(byte[] md5Bytes) {
StringBuffer hexValue = new StringBuffer();
int val=0;
for (int i = 0; i < md5Bytes.length; i++) {
val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
// 测试主函数
public static void main(String args[]) throws NoSuchAlgorithmException {
String s = new String("a");
System.out.println("原始:" + s);
System.out.println("MD5后:" + MD5(s));
}
}