返回顶部
首页 > 资讯 > 后端开发 > JAVA >加密算法---BCryptPasswordEncoder的使用及原理
  • 401
分享到

加密算法---BCryptPasswordEncoder的使用及原理

java 2023-08-16 18:08:04 401人浏览 独家记忆
摘要

BCryptPassWordEncoder的使用及原理 一 介绍二 案例使用2.1 添加依赖2.2 PasswordConfig2.3 application.yml2.4 单元测试2.5 结果 三 优秀博客 一 介绍

一 介绍

spring security中的BCryptPasswordEncoder方法采用SHA-256 +随机盐+密钥对密码进行加密。SHA系列是Hash算法,不是加密算法,使用加密算法意味着可以解密(这个与编码/解码一样),但是采用Hash处理,其过程是不可逆的。
(不可逆加密SHA:
基本原理:加密过程中不需要使用密钥,输入明文后由系统直接经过加密算法处理成密文,这种加密后的数据是无法被解密的,无法根据密文推算出明文。
RSA算法历史:底层-欧拉函数)

1)加密(encode):注册用户时,使用SHA-256+随机盐+密钥把用户输入的密码进行hash处理,得到密码的hash值,然后将其存入数据库中。

2)密码匹配(matches):用户登录时,密码匹配阶段并没有进行密码解密(因为密码经过Hash处理,是不可逆的),而是使用相同的算法把用户输入的密码进行hash处理,得到密码的hash值,然后将其与从数据库中查询到的密码hash值进行比较。如果两者相同,说明用户输入的密码正确。

二 案例使用

2.1 添加依赖

                  org.springframework.security            spring-security-core             5.7.6        

2.2 PasswordConfig

为了防止有人能根据密文推测出salt,我们需要在使用BCryptPasswordEncoder时配置随即密钥,创建一个PasswordConfig配置类,注册BCryptPasswordEncoder对象:

import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import java.security.SecureRandom;@Data@Configuration@ConfigurationProperties(prefix = "encoder.crypt")public class PasswordConfig {        private int strength;        private String secret;    @Bean    public BCryptPasswordEncoder passwordEncoder() {        //System.out.println("secret = " + secret);        //对干扰因子加密        SecureRandom secureRandom = new SecureRandom(secret.getBytes());        //对密码加密        return new BCryptPasswordEncoder(strength, secureRandom);    }}

2.3 application.yml

encoder:  crypt:    secret: ${random.uuid} # 随机的密钥,使用uuid    strength: 6 # 加密强度4~31,决定盐加密时的运算强度,超过10以后加密耗时会显著增加

2.4 单元测试

import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;@SpringBootTestclass ApplicationTests {    final static private String password = "123456";    @Autowired    private BCryptPasswordEncoder encoder;    @Test    void savePassword() {        // encode():对明文字符串进行加密        //注册用户时,使用SHA-256+随机盐+密钥把用户输入的密码进行hash处理,得到密码的hash值,然后将其存入数据库中。        String encode1 = encoder.encode(password);        System.out.println("encode1:" + encode1);        String encode2 = encoder.encode(password);        System.out.println("encode2:" + encode2);        // matches():对加密前和加密后是否匹配进行验证        //用户登录时,密码匹配阶段并没有进行密码解密(因为密码经过Hash处理,是不可逆的),        // 而是使用相同的算法把用户输入的密码进行hash处理,得到密码的hash值,然后将其与从数据库中查询到的密码hash值进行比较。        // 如果两者相同,说明用户输入的密码正确。        boolean matches1 = encoder.matches(password, encode1);        System.out.println("matches1:" + matches1);        boolean matches2 = encoder.matches(password, encode2);        System.out.println("matches2:" + matches2);    }}

2.5 结果

在这里插入图片描述
2.6 项目源码
项目源码

三 优秀博客

SpringSecurity中的BCryptPasswordEncoder算法
SpringSecurity中的密码加密算法:BCryptPasswordEncoder
BCryptPasswordEncoder使用

来源地址:https://blog.csdn.net/weixin_43811057/article/details/128942965

--结束END--

本文标题: 加密算法---BCryptPasswordEncoder的使用及原理

本文链接: https://www.lsjlt.com/news/371849.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作