public static String MD5(String key){
char[] hexDigits={'A','B','C','D','E','F','0','1','2','3','4','5','6','7','8','9'};
try {
byte[] btInput=key.getBytes();
//得到Md5摘要算法的MessageDigest對象
MessageDigest messageDigest=MessageDigest.getInstance("MD5");
//使用指定的字節更新摘要
messageDigest.update(btInput);
//得到密文
byte[] md=messageDigest.digest();
//把密文轉換成十六進制的字符串形式
int i=md.length;
char[] str=new char[i*2];
int j=0;
for (int k=0;k<i;k++){
byte b=md[k];
str[j++]=hexDigits[b>>>4 & 0xf];
str[j++]=hexDigits[b & 0xf];
}
return new String(str);
}catch (Exception e){
return null;
}
}
@Test
public void run() {
String s = MD5();
System.out.println(s);
}