为什么我不能在c#的数据库中使用sha1查询加密密码?

时间:2022-01-22 18:24:11

I have a record in my SQL Sever database like:

我在SQL Sever数据库中有一个记录,比如:

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

My problem is: password after encrypted cannot be found by

我的问题是:加密后的密码不能被发现。

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

in

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());
    }
}

Here is my convert to SHA1 function:

这是我的转换为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;
}

Not find any record. how can I fix this.

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

Thanks a lot!

谢谢!

1 个解决方案

#1


1  

I suggest you to check method that put records in your database, because ToSHA1("123456") == "7c4a8d09ca3762af61e59520943dc26494f8941b7c4a8d09ca3762af61e59520943dc26494f8941b" is false.

我建议你检查一下在你的数据库中记录的方法,因为ToSHA1(“123456”)== " 7c4a8d09ca3762af62af61e5952094f894a8d09ca3762af61e59520943dc26494f8941b "是假的。

Looks like some sort of copy paste issue because ToSHA("123456") is almost a half of your hash: 7c4a8d9ca3762af61e59520943dc26494f8941b

看起来有点像复制粘贴问题,因为ToSHA(“123456”)几乎是您的散列的一半:7c4a8d9ca3762af61e59520943dc26494f8941b。

#1


1  

I suggest you to check method that put records in your database, because ToSHA1("123456") == "7c4a8d09ca3762af61e59520943dc26494f8941b7c4a8d09ca3762af61e59520943dc26494f8941b" is false.

我建议你检查一下在你的数据库中记录的方法,因为ToSHA1(“123456”)== " 7c4a8d09ca3762af62af61e5952094f894a8d09ca3762af61e59520943dc26494f8941b "是假的。

Looks like some sort of copy paste issue because ToSHA("123456") is almost a half of your hash: 7c4a8d9ca3762af61e59520943dc26494f8941b

看起来有点像复制粘贴问题,因为ToSHA(“123456”)几乎是您的散列的一半:7c4a8d9ca3762af61e59520943dc26494f8941b。