BCrypt 加密实现

Bcrypt百度百科:

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

2、bcrypt算法相对来说是运算比较慢的算法,在密码学界有句常话:越慢的算法越安全。算法越算,黑客破解成本越高.通过salt和const这两个值来减缓加密过程,ta的加密时间(百ms级)远远超过md5(大概1ms左右)。对于计算机来说,Bcrypt 的计算速度很慢,但是对于用户来说,这个过程不算慢。bcrypt是单向的,而且经过salt和cost的处理,使其受rainbow攻击破解的概率大大降低,同时破解的难度也提升不少,相对于MD5等加密方式更加安全,而且使用也比较简单.

3、bcrypt加密后的字符串形如:

$2a$10$wouq9P/HNgvYj2jKtUN8rOJJNRVCWvn1XoWy55N3sCkEHZPo3lyWq
其中$是分割符,无意义;2a是bcrypt加密版本号;10是cost的值;而后的前22位是salt值;再然后的字符串就是密码的密文了;这里的const值即下面代码中的saltRounds,生成salt的迭代次数,默认值是10,推荐值12。 除了对您的数据进行加密,默认情况下,bcrypt 在删除数据之前将使用随机数据三次覆盖原始输入文件,以阻挠可能会获得您的计算机数据的人恢复数据的尝试。如果您不想使用此功能,可设定禁用此功能。
bcrypt 使用的是布鲁斯·施内尔在1993年发布的 Blowfish 加密算法。具体来说,bcrypt 使用保罗·柯切尔的算法实现。随 bcrypt 一起发布的源代码对原始版本作了略微改动。
 

简单的说,Bcrypt就是一款加密工具,可以比较方便地实现数据的加密工作。下面是使用Bcrypt对数据加密的一个简单的栗子:

1: 可以官网下载源码,网址:http://www.mindrot.org/projects/jBCrypt/

2: 使用列子

[java] view plain copy
  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3.   
  4. import org.apache.commons.lang3.StringUtils;  
  5. /** 
  6.  * Bcrypt encryption algorithm 
  7.  * @author yue.li3 
  8.  * 
  9.  */  
  10. public class BcryptCipher {  
  11.     // generate salt seed  
  12.     private static final int SALT_SEED = 12;  
  13.     // the head fo salt  
  14.     private static final String SALT_STARTSWITH = "$2a$12";  
  15.       
  16.     public static final String SALT_KEY = "salt";  
  17.       
  18.     public static final String CIPHER_KEY = "cipher";  
  19.       
  20.     /** 
  21.      * Bcrypt encryption algorithm method 
  22.      * @param encryptSource  
  23.      *                  need to encrypt the string 
  24.      * @return Map , two values in Map , salt and cipher 
  25.      */  
  26.     public static Map<String, String> Bcrypt(final String encryptSource) {  
  27.         String salt  = BCrypt.gensalt(SALT_SEED);  
  28.         Map<String, String> bcryptResult = Bcrypt(salt, encryptSource);  
  29.         return bcryptResult;  
  30.     }  
  31.     /** 
  32.      *  
  33.      * @param salt encrypt salt, Must conform to the rules  
  34.      * @param encryptSource 
  35.      * @return 
  36.      */  
  37.     public static Map<String, String> Bcrypt(final String salt, final String encryptSource) {  
  38.         if (StringUtils.isBlank(encryptSource)) {  
  39.             throw new RuntimeException("Bcrypt encrypt input params can not be empty");  
  40.         }  
  41.           
  42.         if (StringUtils.isBlank(salt) || salt.length() != 29) {  
  43.             throw new RuntimeException("Salt can't be empty and length must be to 29");  
  44.         }  
  45.         if (!salt.startsWith(SALT_STARTSWITH)) {  
  46.             throw new RuntimeException("Invalid salt version, salt version is $2a$12");  
  47.         }  
  48.           
  49.         String cipher = BCrypt.hashpw(encryptSource, salt);  
  50.         Map<String, String> bcryptResult = new HashMap<String, String>();  
  51.         bcryptResult.put(SALT_KEY, salt);  
  52.         bcryptResult.put(CIPHER_KEY, cipher);  
  53.         return bcryptResult;  
  54.     }  
  55.       
  56. }  
3:BCrypt 可以官网下载, 下面是我下载好的可以直接用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security提供了多种密码加密方式,其中比较常用的是BCryptPasswordEncoder和MessageDigestPasswordEncoder。 BCryptPasswordEncoder使用BCrypt强哈希函数对密码进行加密,可以通过以下方式在Spring Boot中使用: 1.在pom.xml中添加以下依赖: ``` <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-crypto</artifactId> </dependency> ``` 2.在Spring Security配置类中添加以下代码: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Bean public BCryptPasswordEncoder bCryptPasswordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder()); } } ``` 3.在用户注册时,使用BCryptPasswordEncoder对密码进行加密: ``` @Autowired private BCryptPasswordEncoder bCryptPasswordEncoder; public void register(User user) { user.setPassword(bCryptPasswordEncoder.encode(user.getPassword())); //保存用户信息 } ``` MessageDigestPasswordEncoder使用SHA-256或MD5等哈希算法对密码进行加密,可以通过以下方式在Spring Boot中使用: 1.在pom.xml中添加以下依赖: ``` <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-crypto</artifactId> </dependency> ``` 2.在Spring Security配置类中添加以下代码: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Bean public MessageDigestPasswordEncoder messageDigestPasswordEncoder() { return new MessageDigestPasswordEncoder("MD5"); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(messageDigestPasswordEncoder()); } } ``` 3.在用户注册时,使用MessageDigestPasswordEncoder对密码进行加密: ``` @Autowired private MessageDigestPasswordEncoder messageDigestPasswordEncoder; public void register(User user) { user.setPassword(messageDigestPasswordEncoder.encode(user.getPassword())); //保存用户信息 } ``` 需要注意的是,使用MD5等哈希算法进行密码加密已经逐渐不被推荐,因为这些算法已经被证明存在安全漏洞。建议使用BCryptPasswordEncoder等更加安全的算法进行密码加密

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值