Bcrypt简单理解使用

Bcrypt简介: bcrypt是一种跨平台的文件加密工具。

bcrypt 使用的是布鲁斯·施内尔在1993年发布的 Blowfish 加密算法。

由它加密的文件可在所有支持的操作系统和处理器上进行转移。它的口令必须是8至56个字符,并将在内部被转化为448位的密钥。

Bcrypt就是一款加密工具,可以比较方便地实现数据的加密工作。你也可以简单理解为它内部自己实现了随机加盐处理

例如,我们使用MD5加密,每次加密后的密文其实都是一样的,这样就方便了MD5通过大数据de的方式进行破解。

Bcrypt生成的密文是60位的。而MD5的是32位的。

举个例子看看:

首先这里我使用的maven引入的坐标是【spring-security】的引用

<!--spring-security依赖包-->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
</dependency>

我们编写测试类案例

package test;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author lyc
 * @Description:
 * @create 2018-08-07
 */
public class CeShi {
    public static void main(String[] args) {

        //用户密码
        String password = "123456";
        //密码加密
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        String newPassword = passwordEncoder.encode(password);//加密
        System.out.println("加密密码为:"+newPassword);
        //对比这两个密码是否是同一个密码 
        // true 两个密码一致 false反之
        boolean matches = passwordEncoder.matches(password, newPassword);
        
    }
}

 我们得到的结果是:

加密密码为:$2a$10$iRdNmYoINR8QqynemTsP2OzFtM7N5pFPoBFuzAtvR6YBtov4gRt7e
两个密码是否一致true

我们再运行一遍查看打印结果:

加密密码为:$2a$10$kvc3uoQQDkfxTr3RuVpMJeBd7NZbPMMbma11hp1c68Kseioaqe4U.
两个密码是否一致true

 发现没有两次加密后的密码都是不一样的,即使你再怎么继续加密也没有重复一致的密码。

猜你喜欢

转载自blog.csdn.net/suxiexingchen/article/details/81477142