public static void main(String[] args) {
//生成随机5位随机字符串作为盐
String salt= UUID.randomUUID().toString().replace("-", "").substring(0,5);
System.out.println(salt);
//加密
String password = "admin" + salt;
String encode = encode(password);
System.out.println(encode);
System.out.println(encode(password).equals(encode));
}
public static String encode(String password){
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] b = md.digest(password.getBytes());
for (int i = 0; i < b.length; i++) {
String tmp = Integer.toHexString(b[i] & 0xFF);
if (tmp.length() == 1) {
result += "0" + tmp;
} else {
result += tmp;
}
}
}catch (Exception e){
e.printStackTrace();
}
return result;
}
MD5加盐加密解密
于 2023-03-24 14:38:59 首次发布