ASP.NET MVC 3 应用程序,BCrypt.CheckPassword 失败

4
我正在实施ASP.NET MVC3应用程序的安全性,并使用这里找到的BCrypt实现来处理密码的加密和验证。用户注册屏幕可以很好地加密用户提供的密码,哈希密码也会保存到数据库中。然而,在登录页面上进行密码验证时出现了问题,我似乎无法弄清原因。
我的注册控制器操作包含以下内容:
[HttpPost]
[RequireHttps]
public ActionResult Register(Registration registration)
{
    // Validation logic...

    try
    {
        var user = new User
        {
            Username = registration.Username,
            Password = Password.Hash(HttpUtility.HtmlDecode(registration.Password)),
            EmailAddress = registration.EmailAddress,
            FirstName = registration.FirstName,
            MiddleInitial = registration.MiddleInitial,
            LastName = registration.LastName,
            DateCreated = DateTime.Now,
            DateModified = DateTime.Now,
            LastLogin = DateTime.Now
        };

        var userId = _repository.CreateUser(user);
    }
    catch (Exception ex)
    {
        ModelState.AddModelError("User", "Error creating user, please try again.");
        return View(registration);
    }

    // Do some other stuff...
}

这是 Password.Hash:

public static string Hash(string password)
{
    return BCrypt.HashPassword(password, BCrypt.GenerateSalt(12));
}

这是我处理登录的方式:

[HttpPost]
[RequireHttps]
public ActionResult Login(Credentials login)
{
    // Validation logic...

    var authorized = _repository.CredentialsAreValid(HttpUtility.HtmlDecode(login.username), login.password);
    if (authorized)
    {
        // log the user in...
    }
    else
    {
        ModelState.AddModelError("AuthFail", "Authentication failed, please try again");
        return View(login);
    }
}

CredentialsAreValid将调用BCrypt.CheckPassword进行验证:
public bool CredentialsAreValid(string username, string password)
{
    var user = GetUser(username);
    if (user == null)
        return false;

    return Password.Compare(password, user.Password);
}

Password.Compare:

public static bool Compare(string password, string hash)
{
    return BCrypt.CheckPassword(password, hash);
}

最后,这就是BCrypt.CheckPassword在做什么:

public static bool CheckPassword(string plaintext, string hashed)
{
    return StringComparer.Ordinal.Compare(hashed, HashPassword(plaintext, hashed)) == 0;
}

所以,是的...我不知道发生了什么,但我知道的是,在我的登录控制器操作中,我的布尔变量authorized总是返回false,原因不明。

我在过去的至少几个项目中都使用了这个完全相同的BCrypt类,并且从来没有遇到任何问题。ASP.NET MVC 3是否对发布的数据进行了一些奇怪的、不同的编码,而我又错过了或需要以不同的方式处理它?还是SQL CE 4在这样做(那是我目前正在使用的数据存储)?从我所能看到的代码来看,一切似乎都很正常,但由于某种原因,密码检查每次都失败。有人有什么想法吗?

谢谢。

更新:这里是附带BCrypt类的代码注释,包括如何使用和工作的示例。

/// <summary>BCrypt implements OpenBSD-style Blowfish password hashing
/// using the scheme described in "A Future-Adaptable Password Scheme"
/// by Niels Provos and David Mazieres.</summary>
/// <remarks>
/// <para>This password hashing system tries to thwart offline
/// password cracking using a computationally-intensive hashing
/// algorithm, based on Bruce Schneier's Blowfish cipher. The work
/// factor of the algorithm is parametized, so it can be increased as
/// computers get faster.</para>
/// <para>To hash a password for the first time, call the
/// <c>HashPassword</c> method with a random salt, like this:</para>
/// <code>
/// string hashed = BCrypt.HashPassword(plainPassword, BCrypt.GenerateSalt());
/// </code>
/// <para>To check whether a plaintext password matches one that has
/// been hashed previously, use the <c>CheckPassword</c> method:</para>
/// <code>
/// if (BCrypt.CheckPassword(candidatePassword, storedHash)) {
///     Console.WriteLine("It matches");
/// } else {
///     Console.WriteLine("It does not match");
/// }
/// </code>
/// <para>The <c>GenerateSalt</c> method takes an optional parameter
/// (logRounds) that determines the computational complexity of the
/// hashing:</para>
/// <code>
/// string strongSalt = BCrypt.GenerateSalt(10);
/// string strongerSalt = BCrypt.GenerateSalt(12);
/// </code>
/// <para>
/// The amount of work increases exponentially (2**log_rounds), so
/// each increment is twice as much work. The default log_rounds is
/// 10, and the valid range is 4 to 31.
/// </para>
/// </remarks>

有人找到了答案吗?我似乎遇到了同样的问题。 - user547794
抱歉,我无能为力。最终我不得不切换到使用SHA512并存储盐值。很抱歉我不能提供更多帮助。 - Bob Yexley
3个回答

1

如果我误解了什么,请见谅。看着你的哈希和模型,似乎没有在任何地方存储盐,而是每次都使用新的盐。

因此,当设置密码时,必须同时存储哈希值和盐;当您想要检查输入的密码时,需要检索盐值,使用它计算哈希值,然后与存储的哈希值进行比较。


根据BCrypt类中的代码文档/注释,它不是这样工作的。它的工作方式与.NET框架中的标准SHA1哈希算法不同。我将编辑上面的原始帖子,并包含其中的代码文档。 - Bob Yexley
啊,我明白了,盐值以明文形式存储在哈希中。 - blowdart

1
我遇到了同样的问题。BCryptHelper.CheckPassword 总是返回 false。
我发现哈希字符串在数据库中以 nchar() 的形式存储,导致检查总是失败。 我将其更改为 char(),然后它就可以工作了。

0

在创建用户之前,通常会使用HttpUtility.HtmlDecode()对密码进行初始哈希处理。

Password = Password.Hash(HttpUtility.HtmlDecode(registration.Password)),

然而,在后面将密码与哈希值进行比较时,未使用 HttpUtility.HtmlDecode()。
var authorized = _repository.CredentialsAreValid(HttpUtility.HtmlDecode(login.username), login.password);

或许稍作修改:

var authorized = _repository.CredentialsAreValid(HttpUtility.HtmlDecode(login.username), HttpUtility.HtmlDecode(login.password));

我知道这是一个比较旧的问题,但我正在考虑使用BCrypt,而这个问题引起了我的潜在关注,所以我想知道它是否解决了这个问题。很抱歉,我目前无法验证我的答案,但我希望它能有所帮助。


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