SpringBoot设置md5加密

本文章将介绍不含密码盐以及含有密码盐的md5在SpringBoot中的应用


import java.security.MessageDigest;
import java.util.Base64;

public class EncryptedUtils {
    
    
    private static final String CipherSalt = "1qNcIUyGASa2FQKRxOXALA==";//对应字符==>ninesun

    /**
     * 加密md5
     *
     * @param string
     * @return
     */
    public static String encodeByMd5(String string) {
    
    
        try {
    
    
            // 确定计算方法
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            Base64.Encoder base64Encoder = Base64.getEncoder();
            // 加密字符串
            return base64Encoder.encodeToString(md5.digest(string.getBytes("utf-8")));
        } catch (Exception e) {
    
    
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 判断密码是否能和通过md5加密之后的密码相对应
     *
     * @param password     自己填写的密码
     * @param realPassword 数据库中存储的已经通过md5加密之后的密码
     * @return
     */
    public static Boolean decryptByMd5(String password, String realPassword) {
    
    
        try {
    
    
            //1.加密你自己填写的密码
            password = encodeByMd5(password);
            if (password.equals(realPassword)) {
    
    
                return true;
            } else {
    
    
                return false;
            }

        } catch (Exception e) {
    
    
            e.printStackTrace();
            return false;
        }


    }

    /**
     * 加密md5--含有密码盐
     *
     * @param string
     * @return
     */
    public static String encodeByMd5WithCipher(String string) {
    
    
        try {
    
    
            // 确定计算方法
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            Base64.Encoder base64Encoder = Base64.getEncoder();
            // 加密字符串--在此处添加密码盐
            return (base64Encoder.encodeToString(md5.digest(string.getBytes("utf-8"))) + CipherSalt).replace("=", "");
        } catch (Exception e) {
    
    
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 判断密码是否能和通过md5加密之后的密码相对应--含有密码盐
     *
     * @param password     自己填写的密码
     * @param realPassword 数据库中存储的已经通过md5加密之后的密码
     * @return
     */
    public static Boolean decryptByMd5WithCipher(String password, String realPassword) {
    
    
        try {
    
    
            //1.加密你自己填写的密码
            password = encodeByMd5WithCipher(password);
            if (password.equals(realPassword)) {
    
    
                return true;
            } else {
    
    
                return false;
            }

        } catch (Exception e) {
    
    
            e.printStackTrace();
            return false;
        }


    }

    public static void main(String[] args) {
    
    
        String password = "1234567890";
        System.out.println(encodeByMd5WithCipher(password));
        System.out.println(decryptByMd5WithCipher(password, "LZq3MUFk+Cu44F8FwcAp5g1qNcIUyGASa2FQKRxOXALA"));

    }
}

猜你喜欢

转载自blog.csdn.net/zhiyikeji/article/details/118220199#comments_22779429