md5加密文件:
import java.security.MessageDigest;
public class StringUtil {
@SneakyThrows
public static String md5(String message) {
// 创建MD5消息摘要对象
MessageDigest md = MessageDigest.getInstance("MD5");
// 计算消息的摘要
byte[] digest = md.digest(message.getBytes());
// 将摘要转换为十六进制字符串
String hexString = bytesToHex(digest);
return hexString;
}
private static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}
使用:
String password="123456";
password= StringUtil.md5(password);