代码如下
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Main {
public static void main(String[] args) {
Main Main = new Main();
System.out.println(Main.encryption("xiaowen123"));
}
/**
*
* @param plainText
* 明文
* @return 32位密文
*/
public String encryption(String plainText) {
String md5 = new String();
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));
}
md5 = buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return md5;
}
}
详细说明:http://java.662p.com/thread-3792-1-1.html