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));
    
        }
    }

     三:测试结果

  • 相关阅读:
    兼容ie8 rgba()用法 滤镜filter的用法
    解決BufferedReader读取UTF-8文件中文乱码
    基于JavaScript实现表单密码的隐藏和显示出来
    Java多线程学习(转载)
    使用java 程序创建格式为utf-8文件的方法(写入和读取json文件)
    java获取classpath文件路径空格转变成了转义字符%20的问题
    java中Class.getResource用法
    事务传播行为和特性
    事务隔离级别
    使用Condition循环依次打印123
  • 原文地址:https://www.cnblogs.com/cbpm-wuhq/p/12743143.html
Copyright © 2011-2022 走看看