方法一:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Md5Encoder {
public String encode(String pwd) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] bytes = digest.digest(pwd.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String s = Integer.toHexString(0xff & bytes[i]); //只留后两位
if (s.length() == 1) {
sb.append("0" + s);
} else
sb.append(s);
}
return sb.toString();
}
}