BCrypt.checkpw() 无效的salt版本异常。

26

我正在尝试在我的Play 2.1 Java应用程序中使用BCrypt进行身份验证,但当我尝试对用户进行身份验证时,我遇到了“无效盐版本异常”。

这是我的堆栈跟踪:

play.api.Application$$anon$1: Execution exception[[IllegalArgumentException: Invalid salt version]]
at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.0]
at play.api.DefaultApplication.handleError(Application.scala:383) [play_2.10.jar:2.1.0]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:132) [play_2.10.jar:2.1.0]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:128) [play_2.10.jar:2.1.0]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.0]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.0]
java.lang.IllegalArgumentException: Invalid salt version
at org.mindrot.jbcrypt.BCrypt.hashpw(BCrypt.java:664) ~[jbcrypt-0.3m.jar:na]
at org.mindrot.jbcrypt.BCrypt.checkpw(BCrypt.java:763) ~[jbcrypt-0.3m.jar:na]
at model.operations.DistrictOperations.authenticate(DistrictOperations.java:24) ~[na:na]
at controllers.Application.authenticateDistrict(Application.java:26) ~[na:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.apply(routes_routing.scala:133) ~[na:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.apply(routes_routing.scala:133) ~[na:na]

我正在使用下列的Maven仓库:http://mvnrepository.com/artifact/org.mindrot/jbcrypt/0.3m

我的代码基于文档,因此

district.setPassword(BCrypt.hashpw(json.findPath("password").getTextValue(), BCrypt.gensalt()));    

为了保存密码(同时我也会检查密码是否为空)

BCrypt.checkpw(password, d.getPassword());

用于检查输入的密码是否正确,其中密码是字符串,d.getPassword()是哈希密码。

我不知道这是否是相关信息,但为了准确,我正在使用Hibernate进行ORM和PostgreSQL 8.4作为数据库。

我有些困惑,所以我想问有没有人能帮帮我。非常感谢您的帮助。

10个回答

18

对于其他遇到相同异常的人,请检查是否正确放置了BCrypt.checkpw参数。(我之前没有这样做,因此在意识到自己的愚蠢错误之前找到了这个问题。)

或者按照原帖中的回答,记录/调试哈希密码的值以双重确认您是否正在比较哈希密码!它应该是一个60个字符的字符串,格式为 $2a$10$llw0G6IyibUob8h5XRt9xuRczaGdCm/AiV6SSjf5v78XS824EGbh。


8
特别注意:JBCrypt 期望使用盐的版本为2a,因此盐应以$2a$开头。如果您使用了另一个采用更现代的盐版本(如2y或2b)的BCrypt版本,则JBCrypt会抛出异常。 - daphshez

16

非常抱歉打扰您,我的问题只是代码中一个错误,它保存的是普通字符串而不是BCrypt加密后的字符串。它是从代码的其他部分调用的。


我曾经遇到过类似的问题。我使用了bcrypt.checkpw(hashpass,plainpass)而不是正确的顺序。感觉非常愚蠢。我希望这个评论能让人们再次检查。 - WISERDIVISOR

5
jBcrypt已经过时且没有维护。请考虑切换到新的库实现来处理新版本$2y$。我使用了这个纯Java库https://github.com/patrickfav/bcrypt,将其添加到我的Scala项目中。通过以下函数,我最终可以验证使用VERSION_2Y创建的哈希值:
  /**
    * Verifies an encrypted password against the expected value
    *
    * @link https://github.com/patrickfav/bcrypt
    * @param hash The hashed password (encypted with BCrypt version $2Y$)
    * @param password The unencrypted password string
    */
  private def verifyBcryptHash(hash: String, password: String): Boolean = {
    if (hash == null || hash.trim.isEmpty)
      false
    else
      BCrypt
        .verifyer()
        .verifyStrict(
          password.toCharArray(),
          hash.toCharArray(),
          BCrypt.Version.VERSION_2Y
        )
        .verified
  }

4

我也遇到过同样的问题;请确保你的密码是以哈希格式存储在数据库中,而不是明文。这里有一个Bcrypt生成器,可以将你的明文密码转换成Bcrypt哈希。


2

您需要确保第一个参数是明文,第二个参数是哈希密码。

这是函数的声明:

 public static boolean checkpw(String plaintext, String hashed)

1
< p>如果您传递给checkpw(password, hash)的“hash”值不是可解密的值,BCrypt似乎会抛出这个红色干扰线。


1
在我的情况下,我在插入数据库时使用了{bcrypt}作为前缀。 实例
{bcrypt}$2a$12$Yb3YagKV8B3AXoY2p/Ldk.L2maVKfNlr2dedk4ZUs/YUlalS8EzYu

当我检索密码时,整个值,包括 prefix,将被返回。因此,我已经从 hashing 值中排除了前缀。
String prefix= "{bcrypt}";

String hash_pw= user.getPassword().substring((prefix.length());

BCrypt.checkpw(loginRequest.getPassword(),hash_pw);

我发现 User.withDefaultPasswordEncoder 标注为已弃用,确切输出(如调试器所示)为:{bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG - Sasha Bond
使用PasswordEncoderFactories.createDelegatingPasswordEncoder()来对密码进行编码,并在前缀中指定编码类型。如果您不使用编码机制,则将{noop}作为前缀。 PasswordEncoderFactory通过前缀标识您的哈希类型。 - Nafaz M N M

0

你能把{bcrypt}拆开,然后尝试使用其他细节吗?

BCrypt.checkpw("123", "$2a$10$lVPvO6zyyxEWEPlKBg5B3OTjUHGS4LZ2jlulWAUpOjGz3.helz9H2");

6
欢迎来到StackOverflow。请注意以正确的格式发布您的答案。参考:如何回答问题 - Pawara Siriwardhane

0
在我的情况下,我因为将https://bcrypt-generator.com生成的哈希密码应用到我的服务器的bcrypt检查器(Spring Java安全)中而遇到了这个盐修订错误。然而,使用另一个相同的明文密码但哈希自https://www.javainuse.com/onlineBcrypt,它可以工作并验证密码。记录轮数配置相同(16)。
我认为我的情况是因为使用了不同的bcrypt编码器,尽管有些可能可以与不同的编码器一起使用。
但是,如果bcrypt生成器和检查器来自同一库,则可以确保哈希密码不会出现此问题。

0
昨天在使用bcryptjs时,我遇到了这个错误。我发现错误是因为我的环境变量中定义的salt被传递为字符串,而bcrypt期望的是一个数字。所以我将它转换为数字,问题就解决了。
const hashedPassword = await hash(password, +config.SALT);
// +config.SALT the + sign before the config did the conversion.

希望这对某人有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接