Springboot-实现加密

Springboot-实现加密-使用shiro

md5加密:

非对称加密,使用md5加密:

在pom文件下添加依赖:

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>1.7.0</version>
            <type>bundle</type>
        </dependency>

 单元测试输出:

        String pw = "1230";
        String md5_pw = new Md5Hash(pw).toString();
        System.out.println("md5_pw:  "+md5_pw);

运行结果:

 md5加密已经过时,容易破解

加上盐加密:

即在原密码上添加随机数,这个随机数,就是盐。

        //salt
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();//
        int times = 2;//运算次数
        String algorithmName = "md5"; //algorithmName代表进行加密的算法名称
        String saltpw = new SimpleHash(algorithmName,pw,salt,times).toString();
        System.out.println("原密码"+pw+"  salt:  "+salt+"  saltpw:  "+saltpw);

测试单元输出:

 所以在使用数据库进行存储时,需要增加salt字段

原文地址:https://www.cnblogs.com/djhzzl/p/14170348.html