zoukankan      html  css  js  c++  java
  • Java EncryptionUtils 工具类

    原文地址:http://www.work100.net/tools/code-java/encryption-utils.html
    更多教程:光束云 - 免费课程

    EncryptionUtils

    Java 中加密解密操作工具类。

    主要功能特性:

    • 对文本进行不可逆加密

    • 支持多种加密方式:MD5、SHA256

    • 密码加密自动加入Salt

    源码如下:

    import org.apache.commons.lang3.RandomStringUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.util.DigestUtils;
    
    /**
     * <p>Title: EncryptionUtils</p>
     * <p>Description: </p>
     * <p>Url: http://www.work100.net/tools/code-java/encryption-utils.html</p>
     *
     * @author liuxiaojun
     * @date 2019-08-09 10:08
     * ------------------- History -------------------
     * <date>      <author>       <desc>
     * 2019-08-09   liuxiaojun     初始创建
     * -----------------------------------------------
     */
    public class EncryptionUtils {
    
        private static final int SALT_LENGTH = 6;
        private static final String SEPARATOR = "#";
    
        public enum EncryptionType {
            MD5("md5"),
            SHA256("sha256");
    
            private String type;
    
            EncryptionType(String type) {
                this.type = type;
            }
    
            public String getType() {
                return this.type;
            }
    
            public static EncryptionType getEncryptionType(String type) {
                if (StringUtils.isEmpty(type)) {
                    return MD5;
                }
                for (EncryptionType encryptionType : EncryptionType.values()) {
                    if (encryptionType.type.equalsIgnoreCase(type)) {
                        return encryptionType;
                    }
                }
                return MD5;
            }
        }
    
        /**
         * 加密文本
         *
         * @param encryptionType 加密方式
         * @param sourceText     原文(区分大小写)
         * @return
         */
        public static String encryptText(EncryptionType encryptionType, String sourceText) {
            String encryptedText = "";
            switch (encryptionType) {
                case MD5:
                    encryptedText = DigestUtils.md5DigestAsHex(sourceText.getBytes());
                    break;
                case SHA256:
                    break;
                default:
                    encryptedText = DigestUtils.md5DigestAsHex(sourceText.getBytes());
                    break;
            }
            return encryptedText;
        }
    
        /**
         * 加密密码
         *
         * @param encryptionType 加密方式
         * @param sourcePassword 明文密码
         * @return
         */
        public static String encryptPassword(EncryptionType encryptionType, String sourcePassword) {
            String salt = RandomStringUtils.randomAlphanumeric(SALT_LENGTH);
            sourcePassword = String.format("%s%s", sourcePassword, salt);
            String encryptedPassword = encryptText(encryptionType, sourcePassword);
            return String.format("%s%s%s%s%s", encryptionType.getType(), SEPARATOR, salt, SEPARATOR, encryptedPassword);
        }
    
        /**
         * 验证加密文本
         *
         * @param encryptionType 加密类型
         * @param sourceText     原文
         * @param encryptedText  密文
         * @return
         */
        public static boolean validateEncryptText(EncryptionType encryptionType, String sourceText, String encryptedText) {
            return encryptText(encryptionType, sourceText).equals(encryptedText);
        }
    
        /**
         * 验证密码
         *
         * @param sourcePassword    明文密码
         * @param encryptedPassword 加密后组合串
         * @return
         */
        public static boolean validateEncryptPassword(String sourcePassword, String encryptedPassword) {
            try {
                String[] split = encryptedPassword.split(SEPARATOR);
                EncryptionType encryptionType = EncryptionType.getEncryptionType(split[0]);
                String salt = split[1];
                encryptedPassword = split[2];
                sourcePassword = String.format("%s%s", sourcePassword, salt);
                return encryptText(encryptionType, sourcePassword).equals(encryptedPassword);
            } catch (Exception ex) {
                return false;
            }
        }
    }
    

    如果对课程内容感兴趣,可以扫码关注我们的 公众号QQ群,及时关注我们的课程更新

  • 相关阅读:
    面向对象程序设计(JAVA) 第14周学习指导及要求
    面向对象程序设计(JAVA) 第13周学习指导及要求
    面向对象程序设计(JAVA) 第12周学习指导及要求
    让小球做圆周运动,你有几种办法?
    💥2020面试准备系列(一):JS
    秒懂js的垃圾回收
    jqgrid计算表单
    CSS揭秘实用技巧总结
    利用css 实现 视觉差效果
    前端经典面试题解密:JS的new关键字都干了什么?
  • 原文地址:https://www.cnblogs.com/liuxiaojun/p/tools-code-java-encryption-utils.html
Copyright © 2011-2022 走看看