public static String createMD5(String str) {
StringBuffer stringBuffer = null;
String hexString = null;
try {
//1.指定加密算法类型
MessageDigest digest = MessageDigest.getInstance("MD5");
//2.将需要加密的字符串转换成byte数组
byte[] hash = digest.digest(str.getBytes("UTF-8"));
//3.拼接数据
stringBuffer = new StringBuffer();
for (byte b : hash) {
int i = b&0xFF;
//int类型的i需要转换成16进制
hexString = Integer.toHexString(i);
if(hexString.length()<2) {
hexString = "0"+hexString;
}
stringBuffer.append(hexString);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException("MD5 should be supported?", e);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new RuntimeException("UTF-8 should be supported?", e);
}
return stringBuffer.toString();
}
java对字符串进行MD5加密
最新推荐文章于 2024-06-30 06:00:00 发布