为什么我不能在 C# 中通过 sha1 从数据库中查询加密密码

Why can't I query encrypted password by sha1 from Database in C#

我的 SQL 服务器数据库中有一条记录,例如:

Use_Name: admin1 
// see comment on answer below: this hash is wrong. 
Use_Password: 7c4a8d09ca3762af61e59520943dc26494f8941b7c4a8d09ca3762af61e59520943dc26494f8941b
reverses -> 123456

我的问题是:

无法找到加密后的密码
var user = db.Users.Where(b => b.Use_Name == name)
                   .Where(b => b.Use_Password == passSHA1)
                   .ToList<User>();

public bool Login(string name, string password)
{
    try
    {
       string passSHA1;
       passSHA1 = ToSHA1(password);

       var user = db.Users.Where(b => b.Use_Name == name)
                          .Where(b => b.Use_Password == passSHA1)
                          .ToList<User>();

       if (user.Any())
       {
          return true;
       }
       else
       {
          return false;
       }
    }
    catch (Exception ex)
    {
        return false;
        throw new Exception("UserBO.Login" + ex.ToString());
    }
}

这是我转换为 SHA1 的函数:

private string ToSHA1 (string pass) {
    SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
    byte[] bs = System.Text.Encoding.UTF8.GetBytes(pass);
    bs = sha1.ComputeHash(bs);
    System.Text.StringBuilder s = new System.Text.StringBuilder();

    foreach (byte b in bs) {
         s.Append(b.ToString("x1").ToLower());
    }

    pass = s.ToString();
    return pass;
}

没有找到任何记录。我该如何解决这个问题。

非常感谢!

我建议您检查将记录放入数据库的方法,因为 ToSHA1("123456") == "7c4a8d09ca3762af61e59520943dc26494f8941b7c4a8d09ca3762af61e59520943dc26494f8941b"false

看起来像是某种复制粘贴问题,因为 ToSHA("123456") 几乎是您的哈希值的一半:7c4a8d9ca3762af61e59520943dc26494f8941b