一、示例方法
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* 获取md5加密的字符串
*
* @param string 输入字符串
* @return 加密后的字符串
*/
public static String getMd5(String string) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
byte[] md5Bytes = null;
StringBuilder md5Str = new StringBuilder();
if (null != md5) {
md5Bytes = md5.digest(string.getBytes(StandardCharsets.UTF_8));
for (byte md5Byte : md5Bytes) {
md5Str.append(Integer.toHexString(md5Byte | 0xFFFFFF00).substring(6));
}
}
return md5Str.toString();
}