spring security加密
参考:https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/crypto/password/PasswordEncoder.html
以上连接在官网api中可查到所有加密方式,通常使用的2种即可:
第一种:BCryptPasswordEncoder:这种加密实现是单向hash加密,特点是:每次生成的密码都不同,自动撒盐。缺点:可通过hash碰撞破解(加密时间比较慢所以hash碰撞破解也很吃力),可通过彩虹表破解,这就没办法了。所以这种加密还是会被破解只不过相对其他方式破解的代价比较高。
第二种:Pbkdf2PasswordEncoder:这种加密实现一样是不可逆的,特点:每次生成的密码都不一样,基本无法破解。缺点:生成密码的时间比较长,因为可以在构造器传入专属秘钥(字符串),这样就能得到一个自己的加密器。所以针对这种情况彩虹表理论上可以破解,但难度成几何倍增长。所以彩虹表破解可忽略。
public static void main(String[] args) { String password = "jia123?00/\\"; //单向hash,密码不可逆,破解方式可以获得盐后进行哈希碰撞,也可用彩虹表。 //参数:BCryptVersion 加密算法的版本,默认 $2A,除此之外可选的有$2Y和$2B; //参数:strength 个人理解的是单向hash的次数,默认值是10,可选4打31位; //参数:random安全的随机数生成器,不指定的话,就是 BCrypt.gensalt(version.getVersion(), strength) 方法来生成 PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); System.out.println(password); System.out.println(passwordEncoder.encode(password)); System.out.println(passwordEncoder.matches(password, "$2a$10$EA8hJWwmZ7X/vu3LNtQPG.zmb6xfR9mqHuBxfQ7dwlRv8TFsa4oVa")); //同样是撒盐后进行加密,可以传入一个秘钥,这样就是独立的加密器,即使别人获得数据库的密文也不会破解。 //特点:加密时间比较长,所以hash很难破解与BCryptPasswordEncoder相同。另外因为有秘钥彩虹表也破解不了。 PasswordEncoder passwordEncoder2 = new Pbkdf2PasswordEncoder("password"); String pd2 = passwordEncoder2.encode(password); System.out.println(pd2); System.out.println("bd2:" + passwordEncoder2.matches(password, pd2)); }
本文来自博客园,作者:~~mr.li~~,转载请注明原文链接:https://www.cnblogs.com/li-yan-long/p/15753863.html

浙公网安备 33010602011771号