MD5 salt encryption tools (can be used directly)

MD5 salt encryption tools

Insert image description here

When we are working on projects, user passwords are involved. Normally, plain text passwords are not stored directly in the database, but are all encrypted passwords.

There are many ways to encrypt passwords, such as:

① 3DES, AES, DES: Using symmetric encryption algorithm, the original password can be restored through decryption

② MD5, SHA1: Using one-way HASH algorithm, the original password cannot be restored through calculation, but a rainbow table can be established for table lookup and cracking

The simple MD5 decryption method requires the use of this website (interested friends can try it yourself), free MD5 decryption website:https://md5.cn/


Can be done using MD5With saltEncryption to ensure security. Let me share with you the MD5 salt encryption tools used in the project.

  • Encrypt and salt passwords
public static String getEncryptedPwd(String password)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
    
    
        // 声明加密后的口令数组变量
        byte[] pwd = null;
        // 随机数生成器
        SecureRandom random = new SecureRandom();
        // 声明盐数组变量
        byte[] salt = new byte[SALT_LENGTH];
        // 将随机数放入盐变量中
        random.nextBytes(salt);
        // 声明消息摘要对象
        MessageDigest md = null;
        // 创建消息摘要
        md = MessageDigest.getInstance("MD5");
        // 将盐数据传入消息摘要对象
        md.update(salt);
        // 将口令的数据传给消息摘要对象
        md.update(password.getBytes("UTF-8"));
        // 获得消息摘要的字节数组
        byte[] digest = md.digest();

        // 因为要在口令的字节数组中存放盐,所以加上盐的字节长度
        pwd = new byte[digest.length + SALT_LENGTH];
        // 将盐的字节拷贝到生成的加密口令字节数组的前12个字节,以便在验证口令时取出盐
        System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);
        // 将消息摘要拷贝到加密口令字节数组从第13个字节开始的字节
        System.arraycopy(digest, 0, pwd, SALT_LENGTH, digest.length);
        // 将字节数组格式加密后的口令转化为16进制字符串格式的口令
        return byteToHexString(pwd);
    }
  • Verify password (pass in password when logging in)
public static boolean validPassword(String password, String passwordInDb)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
    
    
        // 将16进制字符串格式口令转换成字节数组
        byte[] pwdInDb = hexStringToByte(passwordInDb);
        // 声明盐变量
        byte[] salt = new byte[SALT_LENGTH];
        // 将盐从数据库中保存的口令字节数组中提取出来
        System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
        // 创建消息摘要对象
        MessageDigest md = MessageDigest.getInstance("MD5");
        // 将盐数据传入消息摘要对象
        md.update(salt);
        // 将口令的数据传给消息摘要对象
        md.update(password.getBytes());
        // 生成输入口令的消息摘要
        byte[] digest = md.digest();
        // 声明一个保存数据库中口令消息摘要的变量
        byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH];
        // 取得数据库中口令的消息摘要
        System.arraycopy(pwdInDb, SALT_LENGTH, digestInDb, 0, digestInDb.length);
        // 比较根据输入口令生成的消息摘要和数据库中消息摘要是否相同
        if (Arrays.equals(digest, digestInDb)) {
    
    
            // 口令正确返回口令匹配消息
            return true;
        } else {
    
    
            // 口令不正确返回口令不匹配消息
            return false;
        }
    }
  • The complete tool class code is as follows
package com.codingfuture.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;

public class MD5SaltUtil {
    
    
    private static final String HEX_NUMS_STR = "0123456789ABCDEF";
    private static final Integer SALT_LENGTH = 12;

    //将16进制字符转换为字节数组
    public static byte[] hexStringToByte(String hex) {
    
    
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] hexChars = hex.toCharArray();
        for (int i = 0; i < len; i++) {
    
    
            int pos = i * 2;
            result[i] = (byte) (HEX_NUMS_STR.indexOf(hexChars[pos]) << 4 | HEX_NUMS_STR
                    .indexOf(hexChars[pos + 1]));
        }
        return result;
    }


    //将指定byte数组转换成16进制字符串
    public static String byteToHexString(byte[] b) {
    
    
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
    
    
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
    
    
                hex = '0' + hex;
            }
            hexString.append(hex.toUpperCase());
        }
        return hexString.toString();
    }

    //验证口令是否合法
    public static boolean validPassword(String password, String passwordInDb)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
    
    
        // 将16进制字符串格式口令转换成字节数组
        byte[] pwdInDb = hexStringToByte(passwordInDb);
        // 声明盐变量
        byte[] salt = new byte[SALT_LENGTH];
        // 将盐从数据库中保存的口令字节数组中提取出来
        System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
        // 创建消息摘要对象
        MessageDigest md = MessageDigest.getInstance("MD5");
        // 将盐数据传入消息摘要对象
        md.update(salt);
        // 将口令的数据传给消息摘要对象
        md.update(password.getBytes());
        // 生成输入口令的消息摘要
        byte[] digest = md.digest();
        // 声明一个保存数据库中口令消息摘要的变量
        byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH];
        // 取得数据库中口令的消息摘要
        System.arraycopy(pwdInDb, SALT_LENGTH, digestInDb, 0, digestInDb.length);
        // 比较根据输入口令生成的消息摘要和数据库中消息摘要是否相同
        if (Arrays.equals(digest, digestInDb)) {
    
    
            // 口令正确返回口令匹配消息
            return true;
        } else {
    
    
            // 口令不正确返回口令不匹配消息
            return false;
        }
    }

    //获得加密后的口令
    public static String getEncryptedPwd(String password)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
    
    
        // 声明加密后的口令数组变量
        byte[] pwd = null;
        // 随机数生成器
        SecureRandom random = new SecureRandom();
        // 声明盐数组变量
        byte[] salt = new byte[SALT_LENGTH];
        // 将随机数放入盐变量中
        random.nextBytes(salt);

        // 声明消息摘要对象
        MessageDigest md = null;
        // 创建消息摘要
        md = MessageDigest.getInstance("MD5");
        // 将盐数据传入消息摘要对象
        md.update(salt);
        // 将口令的数据传给消息摘要对象
        md.update(password.getBytes("UTF-8"));
        // 获得消息摘要的字节数组
        byte[] digest = md.digest();

        // 因为要在口令的字节数组中存放盐,所以加上盐的字节长度
        pwd = new byte[digest.length + SALT_LENGTH];
        // 将盐的字节拷贝到生成的加密口令字节数组的前12个字节,以便在验证口令时取出盐
        System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);
        // 将消息摘要拷贝到加密口令字节数组从第13个字节开始的字节
        System.arraycopy(digest, 0, pwd, SALT_LENGTH, digest.length);
        // 将字节数组格式加密后的口令转化为16进制字符串格式的口令
        return byteToHexString(pwd);
    }
}

To learn more about password encryption methods, you can read the following blogger’s article:
https://blog.csdn.net/ZGL_cyy/article/details/113826689?ops_request_misc =%257B%2522request%255Fid%2522%253A%2522166089218916782391890906%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%2 57D&request_id=166089218916782391890906&biz_id=0&utm_medium=distribute.pc_search_result.none -task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-7-113826689-null-null.142v42control&utm_term=%E5%AF%86%E7%A0%81%E5%8A% A0%E5%AF%86&spm=1018.2226.3001.4187v2control,185
Insert image description here

Supongo que te gusta

Origin blog.csdn.net/xiangyuWA/article/details/126425292
Recomendado
Clasificación