zoukankan      html  css  js  c++  java
  • Shiro 加密与解密 util

    Shiro 加密与解密 util

    一:环境

    需要的环境已标红,其他的酌情选择

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>11</java.version>
    
            <shiro.version>1.4.0</shiro.version>
    
        </properties>
            <!--shiro begin-->
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-web</artifactId>
                <version>${shiro.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-cas</artifactId>
                <version>${shiro.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-core</artifactId>
                <version>${shiro.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-spring</artifactId>
                <version>${shiro.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-ehcache</artifactId>
                <version>${shiro.version}</version>
            </dependency>
    
            <dependency>
                <groupId>com.github.theborakompanioni</groupId>
                <artifactId>thymeleaf-extras-shiro</artifactId>
                <version>2.0.0</version>
            </dependency>
    
            <!--shiro end-->

    二:CryptographyUtil.java 加密封装类

    import时一定要引入org.apache.shiro.下的包,jdk和common都有加密的包,容易引入错误

    package com.kexin.common.util.encry;
    
    import org.apache.shiro.codec.Base64;
    import org.apache.shiro.crypto.hash.Md5Hash;
    
    /**
     * shiro 加密方法
     */
    public class CryptographyUtil {
    
        private static final String DEFAULT_URL_ENCODING = "UTF-8"; //默认的加密编码
        /**
         * Md5加密,带盐
         * @param str
         * @param salt
         * @return
         */
        public static String md5(String str, String salt){
            return new Md5Hash(str,salt).toString();
        }
    
        /**
         * Md5加密,不带盐
         * @param str
         * @return
         */
        public static String md5NotSalt(String str){
            return  new Md5Hash(str).toString();
        }
    
        /**
         * base加密
         * @param input
         * @return
         */
        public static String encodeBase64(String input) {
            return Base64.encodeToString(input.getBytes());
        }
    
        /**
         * base64解密
         * @param input
         * @return
         */
        public static String decodeBase64String(String input) {
            return Base64.decodeToString(input.getBytes());
        }
    
        public static void main(String[] args) {
            String password="123456";
            
            System.out.println("Md5加密带盐:"+ CryptographyUtil.md5(password, "Admin"));
            System.out.println("Md5加密不带盐:"+ CryptographyUtil.md5NotSalt("Admin"));
    
            String encodeBase64String=CryptographyUtil.encodeBase64(password);
            System.out.println("base64加密:"+ encodeBase64String);
            System.out.println("base64解密:"+ CryptographyUtil.decodeBase64String(encodeBase64String));
    
        }
    }

     三:测试结果

  • 相关阅读:
    饿了么P7级前端工程师进入大厂的面试经验
    前端程序员面试的坑,简历写上这一条信息会被虐死!
    这次来分享前端的九条bug吧
    移动端开发必会出现的问题和解决方案
    创建一个dynamics 365 CRM online plugin (八)
    创建一个dynamics 365 CRM online plugin (七)
    创建一个dynamics 365 CRM online plugin (六)
    创建一个dynamics 365 CRM online plugin (五)
    使用User Primary Email作为GUID的问题
    怎样Debug Dynamics 365 CRM Plugin
  • 原文地址:https://www.cnblogs.com/cbpm-wuhq/p/12743143.html
Copyright © 2011-2022 走看看