java md5加密解密工具类
时间: 2023-09-23 10:02:59 浏览: 112
下面是一个简单的JavaMD5加密解密工具类:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {
public static String md5(String text) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(text.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < digest.length; i++) {
String hex = Integer.toHexString(0xff & digest[i]);
if (hex.length() == 1) sb.append('0');
sb.append(hex);
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}
使用方法:
String text = "hello";
String md5 = MD5Utils.md5(text);
System.out.println(md5);
输出结果:
5d41402abc4b2a76b9719d911017c592
阅读全文