测试过,和网上加密后的字符串相同
package com.test;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Test7 {
public static void main(String args[]){
Md5("password");
}
private static String Md5(String plainText){
String result="";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.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));
}
System.out.println("result: " + buf.toString());// 32λ�ļ���
System.out.println("result: " + (result=buf.toString().substring(8, 24)));// 16λ�ļ���
} catch (NoSuchAlgorithmException e){
e.printStackTrace();
}
return result;
}
}