• Java中使用 MD5 工具进行对密码进行加密


    基于MD5 的双重加密

    package com.goods.springbootmybatisgoods.util;
    
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    /**
     * MD5加密工具类
     */
    public class MD5Util {
        /**
         * 生成盐值
         * @param source 需要加密的字符串
         * @return 加密后的字符串
         */
        public static String md5(String source) {
            try {
                // 创建MD5消息摘要对象
                MessageDigest md = MessageDigest.getInstance("MD5");
    
                // 将输入字符串转换为字节数组
                byte[] inputBytes = source.getBytes();
    
                // 计算MD5摘要
                byte[] md5Bytes = md.digest(inputBytes);
    
                // 将字节数组转换为16进制字符串
                StringBuilder sb = new StringBuilder();
                for (byte b : md5Bytes) {
                    sb.append(String.format("%02x", b));
                }
    
                String md5Hash = sb.toString();
    
                System.out.println("MD5 Hash: " + md5Hash);
                return md5Hash;
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * 生成盐值
         * @param source 需要加密的字符串
         * @param salt 盐值
         * @return 加密后的字符串
         */
        public static String md5(String source, String salt) {
            try {
                byte[] saltByte = salt.getBytes();
                // 创建MD5消息摘要对象
                MessageDigest md = MessageDigest.getInstance("MD5");
    
                // 将密码和盐值连接
                byte[] passwordWithSalt = concatenateByteArrays(source.getBytes(), saltByte);
    
                // 计算MD5摘要
                byte[] md5Bytes = md.digest(passwordWithSalt);
    
                // 将字节数组转换为16进制字符串
                StringBuilder sb = new StringBuilder();
                for (byte b : md5Bytes) {
                    sb.append(String.format("%02x", b));
                }
                return sb.toString();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            return source;
        }
    
        private static byte[] concatenateByteArrays(byte[] a, byte[] b) {
            byte[] result = new byte[a.length + b.length];
            System.arraycopy(a, 0, result, 0, a.length);
            System.arraycopy(b, 0, result, a.length, b.length);
            return result;
        }
    
        /**
         * 加密密码 两次MD5
         * @param password 密码
         * @param salt 盐值
         * @return 加密后的密码
         */
        public static String encodePassword(String password, String salt) {
            return md5(md5(password, salt));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
  • 相关阅读:
    kubernetes
    来看下这篇文章,教你如何实现一个SpringBoot的Mybatis分库分表组件
    k8s的pod内部打包工程镜像
    论文阅读:Explainability for Large Language Models: A Survey
    PaddleNLP UIE -- 药品说明书信息抽取(名称、规格、用法、用量)
    工作数字化的中国历程 | 从 OA 到 BPM 到数字流程自动化
    使用ExcelJS快速处理Node.js爬虫数据
    Mybatis中的关系映射
    如何一站式解决跨境电商难题?
    halcon 提取图像中的感兴趣区域
  • 原文地址:https://blog.csdn.net/Best_Basic/article/details/134255181