springsecurity中的BCrypt加密算法

一、简介

1️⃣BCrypt加密:一种加盐的单向Hash,不可逆的加密算法,同一种明文,每次加密后的密文都不一样,而且不可反向破解生成明文,破解难度很大。每次加密的时候首先会生成一个随机数就是盐,之后将这个盐值与明文密码进行hash,得到 一个hash值存到数据库中。其中生成的hash值中包含了之前生成的盐值(22个字符),用于后续hash值验证。

2️⃣MD5加密:是不加盐的单向Hash,不可逆的加密算法,同一个密码经过hash的时候生成的是同一个hash值,在大多数的情况下,有些经过md5加密的方法将会被破解。

二、过程

1️⃣生成盐值salt。调用BCrypt类的构造函数,具体如下:

BCrypt.gensalt();
BCrypt.gensalt(String strength);
BCrypt.gensalt(String strength, SecureRandom secureRandom );

其中strength是散列数因子,范围是4到31,默认是10。random是随机数产生器,也就是new SecureRandom()这个。

2️⃣生成加密字符串hashed。调用BCrypt类的hashpw(String password, String salt)函数。其中password是明文密码,salt是第一步生成的盐值。

BCrypt.hashpw(rawPassword.toString(), salt)

3️⃣验证密文是否正确。调用BCrypt类的checkpw(String plaintext, String hashed)函数。其中plaintext是需要传入的明文,hashed是第二步生成的密文。

BCrypt.checkpw(rawPassword.toString(), encodedPassword)

该方法返回布尔类型,true表示验证通过,false表示不通过。

进一步解析checkpw(String plaintext, String hashed)函数:

public static boolean checkpw(String plaintext, String hashed) {
    return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));
}
static boolean equalsNoEarlyReturn(String a, String b) {
    char[] caa = a.toCharArray();
    char[] cab = b.toCharArray();
    if (caa.length != cab.length) {
        return false;
    }
    byte ret = 0;
    for (int i = 0; i < caa.length; i++) {
        ret |= caa[i] ^ cab[i];
    }
    return ret == 0;
}

源码中可以看出,先是把密文hashed当作盐值,和明文plaintext做加密算法,算法函数是hashpw(plaintext, hashed);。返回的加密结果跟之前计算的加密结果做对比,对比函数是equalsNoEarlyReturn(String a, String b);。一样则true表示验证通过,否则验证不通过。

三、BCryptPasswordEncoder

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
public class Test {
    public static void main(String[] args) {

        String password = "123456";
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(6);
        String hashedPassword1 = passwordEncoder.encode(password);
        String hashedPassword2 = passwordEncoder.encode(password);
        String hashedPassword3 = passwordEncoder.encode(password);
        String hashedPassword4 = passwordEncoder.encode(password);
        System.out.println(hashedPassword1);
        System.out.println(hashedPassword2);
        System.out.println(hashedPassword3);
        System.out.println(hashedPassword4);

        boolean match1 = passwordEncoder.matches(password, hashedPassword1);
        System.out.println(match1);
        boolean match2 = passwordEncoder.matches(password, hashedPassword2);
        System.out.println(match2);
        boolean match3 = passwordEncoder.matches(password, hashedPassword3);
        System.out.println(match3);
        boolean match4 = passwordEncoder.matches(password, hashedPassword4);
        System.out.println(match4);
    }
}

结果如下:

$2a$06$.PnAp0jsfeTND2yBjdfFcuV0VoNQ6.E5hHDxWRrmGDuGxV3bj/zUS
$2a$06$Juq9dexQqveFiJhcPqZ/SeuEGB8/BtLMyCpYBNEfnm4F2HG/FFK9y
$2a$06$yCT1TPwWOpWr5.OrAdis5OOvbCOSmBuadibbxO7zajSChsAwFC8R6
$2a$06$cCMPkgZc7s2lB06fWG6SnO8nsZvBwXr0eZZFkLo16pLjk8A/XN196
true
true
true
true

登录注册还是用https安全。在客户端base64或者md5什么的真心没用,直接监听到所谓的密文,然后用脚本发起一个http请求就可以登录上去了。http在网络上是明文传输的,代理和网关都能看到所有的数据,在同一局域网内也可以被嗅探到,可以开个wireshark抓下局域网的包试试看。我认为加密也没有提高什么攻击难度,因为攻击者就没必要去解密原始密码,能登录上去就表示目标已经实现了,所以难度没有提高。另外,在客户端md5后,服务端怎么把原始密码还原出来,不能数据库直接存md5吧?所以要选择加密算法的话,还要让服务端能还原出来原始密码。然后,如果是简单的base64下,这种算法对安全性没什么提高,base64又不是加密算法,它的作用就是编码而已。其他的诸如非对称加密之类的算法当然可以,但是这不是https提供的功能么?而且https提供的安全保障还可以应对其他的攻击。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容