8、MD5工具类
MD5的全称是Message Digest Algorithm 5(信息摘要算法)
MD5摘要算法被广泛应用在密码信息安全,之后被证实无法防止碰撞,初学者通过了解MD5,可初步了解安全性认证在程序中的重要性
//确定算法方法
MessageDigest md5=MessageDigest.gettlnstance(“MD5”)
//JDK1.8新增了Base64
String newstr=Base64.getEncoder.encodeToString(md5.digest(str.getBytes(“utf-8”)));
import java.security.*;
import java.util.Arrays;
import java.util.Base64;
public class MD5 {
public static void main(String[] args) {
String password="admin12345"; //明文(原文)
String savePasseord="dIjjMbi2TleU2j+k6xCtXQ==";//存储的密文
try {
MessageDigest md=MessageDigest.getInstance("md5");
//通过MD5计算摘要
byte[] bytes=md.digest(password.getBytes("UTF-8"));
System.out.println(Arrays.toString(bytes));
String mdStr=new String(bytes);
//System.out.println(mdStr);
//a-z A-Z / *BASE64编码算法
//1.8
String str=Base64.getEncoder().encodeToString(bytes);
System.out.println(str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.util.Arrays;
import java.util.Base64;
public class MD5 {
private static String savePassword="dIjjMbi2TleU2j+k6xCtXQ==";
public static void main(String[] args) {
System.out.println( login("admin12345"));
}
private static boolean login(String password) {
if(savePassword.equals(md5(password))) {
return true;
}else {
return false;
}
}
private static String md5(String password) {
try {
MessageDigest md=MessageDigest.getInstance("md5");
byte[] bytes=md.digest(password.getBytes("UTF-8"));
String str=Base64.getEncoder().encodeToString(bytes);
return str;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
true