BCrypt加密方式
用户表的密码通常使用MD5等不可逆算法加密后存储,为防止彩虹表破解更会先使用一个特定的字符串(如域名)加密,然后再使用一个随机的salt(盐值)加密。 特定字符串是程序代码中固定的,salt是每个密码单独随机,一般给用户表加一个字段单独存储,比较麻烦。 BCrypt算法将salt随机并混入最终加密后的密码,验证时也无需单独提供之前的salt,从而无需单独处理salt问题。相比较MD5来说,其加密型更强,MD5需要自己手动将salt进行搭配加密,已达到统一密码不同密文的效果,
而BCript直接可使用加密后为64为字符串.
话不多说,直接上代码
方式一:
String gensalt = BCrypt.gensalt();
System.out.println("盐:"+gensalt);
//基于当前的盐对密码进行加密
String saltPassword = BCrypt.hashpw("123456", gensalt);
System.out.println("加密后的密文:"+saltPassword);
//解密
boolean checkpw = BCrypt.checkpw("123456", saltPassword);
System.out.println("密码校验结果:"+checkpw);
方式二:
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String encode = encoder.encode("123456");
System.out.println(encode);
boolean matches = encoder.matches("123456",
"$2a$10$0vslpoaBsAZo5h.vUhICgOVb3BbceGh2P8NLvEhkwOGy4eR/5xhl6");
System.out.println(matches);
以上两种方式,都可以进行明文加密,有兴趣的小伙伴可以试一下