散列出错

发布于 2024-12-08 08:14:07 字数 3987 浏览 3 评论 0原文

我使用相同的函数来哈希值以在登录期间进行比较,就像我在用户注册时哈希密码一样:

Public Shared Function Compute(ByVal text As String, ByVal algorithm As String, Optional ByVal salt() As Byte = Nothing) As String
    If salt Is Nothing Then
        Dim saltSize As Integer = 8
        salt = New Byte(saltSize - 1) {}

        Dim rng As New RNGCryptoServiceProvider
        rng.GetNonZeroBytes(salt)
    End If

    Dim textBytes As Byte() = Encoding.UTF8.GetBytes(text)
    Dim saltedTextBytes() As Byte = New Byte(textBytes.Length + salt.Length - 1) {}
    For i As Integer = 0 To textBytes.Length - 1
        saltedTextBytes(i) = textBytes(i)
    Next i

    For i As Integer = 0 To salt.Length - 1
        saltedTextBytes(textBytes.Length + i) = salt(i)
    Next i

    Dim hash As HashAlgorithm
    If algorithm Is Nothing Then
        algorithm = ""
    End If

    Select Case algorithm.ToUpper
        Case "SHA1" : hash = New SHA1Managed
        Case "SHA256" : hash = New SHA256Managed
        Case "SHA384" : hash = New SHA384Managed
        Case "SHA512" : hash = New SHA512Managed
        Case Else : hash = New MD5CryptoServiceProvider
    End Select

    Dim hashBytes As Byte() = hash.ComputeHash(saltedTextBytes)
    Dim saltedHash() As Byte = New Byte(hashBytes.Length + salt.Length - 1) {}
    For i As Integer = 0 To hashBytes.Length - 1
        saltedHash(i) = hashBytes(i)
    Next i

    For i As Integer = 0 To salt.Length - 1
        saltedHash(hashBytes.Length + i) = salt(i)
    Next i

    Dim hashValue As String = Convert.ToBase64String(saltedHash)

    Return Left(hashValue, 36)
End Function

我的问题是,当我尝试登录其密码已通过此函数哈希的帐户时,哈希值不会不匹配。我想我跳过了一个步骤或其他什么。

以下是创建用户帐户的代码:

        ' The email address needs to be valid
        Dim pattern As String = "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
        Dim match As Match = Regex.Match(txtEmail.Text, pattern)
        If match.Success Then
            'Hash the user's password before entering it into the database.
            Dim pass As String = Crypt.Compute(txtPass.Text, "SHA512", Nothing)

            ' Enter the information from the form into the database.
            Dim sql As String = "INSERT INTO Users(Username, Password, EmailAddress) " & _
                "VALUES(@User, @Pass, @Email)"
            Dim cmd As New SqlCommand(sql, conn)
            cmd.Parameters.AddWithValue("@User", txtName.Text)
            cmd.Parameters.AddWithValue("@Pass", pass)
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text)

            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
        Else
            lblError.Text = "Invalid email address. Please correct."
            lblError.ForeColor = Drawing.Color.Red
        End If

这里未包含更多与我的问题无关的检查。

这是我的用户登录:

            Dim pass As String = Crypt.Compute(txtPass.Text, "SHA512", Nothing)

            Dim UserData As New DataSet
            Dim UserAdapter As New SqlDataAdapter
            UserAdapter.SelectCommand = New SqlCommand("SELECT * FROM Users " & _
                                                       "WHERE Username = @User AND Password = @Pass", conn)
            UserAdapter.SelectCommand.Parameters.AddWithValue("@User", txtUser.Text)
            UserAdapter.SelectCommand.Parameters.AddWithValue("@Pass", pass)
            UserAdapter.Fill(UserData)

            If UserData.Tables(0).Rows.Count <> 1 Then
                lblError.Text = "Invalid username or password."
                lblError.ForeColor = Drawing.Color.Red
                Session("LoginAttempt") = CInt(Session("LoginAttempt")) + 1
            Else
                Session("LoggedIn") = True
                Response.Redirect("Home.aspx")
            End If

据我所知,我在这里所做的哈希没有任何区别。

有人有什么想法吗?

I'm using the same function to hash values for comparison during login as I am to hash the passwords when users register:

Public Shared Function Compute(ByVal text As String, ByVal algorithm As String, Optional ByVal salt() As Byte = Nothing) As String
    If salt Is Nothing Then
        Dim saltSize As Integer = 8
        salt = New Byte(saltSize - 1) {}

        Dim rng As New RNGCryptoServiceProvider
        rng.GetNonZeroBytes(salt)
    End If

    Dim textBytes As Byte() = Encoding.UTF8.GetBytes(text)
    Dim saltedTextBytes() As Byte = New Byte(textBytes.Length + salt.Length - 1) {}
    For i As Integer = 0 To textBytes.Length - 1
        saltedTextBytes(i) = textBytes(i)
    Next i

    For i As Integer = 0 To salt.Length - 1
        saltedTextBytes(textBytes.Length + i) = salt(i)
    Next i

    Dim hash As HashAlgorithm
    If algorithm Is Nothing Then
        algorithm = ""
    End If

    Select Case algorithm.ToUpper
        Case "SHA1" : hash = New SHA1Managed
        Case "SHA256" : hash = New SHA256Managed
        Case "SHA384" : hash = New SHA384Managed
        Case "SHA512" : hash = New SHA512Managed
        Case Else : hash = New MD5CryptoServiceProvider
    End Select

    Dim hashBytes As Byte() = hash.ComputeHash(saltedTextBytes)
    Dim saltedHash() As Byte = New Byte(hashBytes.Length + salt.Length - 1) {}
    For i As Integer = 0 To hashBytes.Length - 1
        saltedHash(i) = hashBytes(i)
    Next i

    For i As Integer = 0 To salt.Length - 1
        saltedHash(hashBytes.Length + i) = salt(i)
    Next i

    Dim hashValue As String = Convert.ToBase64String(saltedHash)

    Return Left(hashValue, 36)
End Function

My problem is that when I try to log in on an account whose password was hashed by this function, the hashed values don't match up. I think I'm skipping a step or something.

Here's the code for user account creation:

        ' The email address needs to be valid
        Dim pattern As String = "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
        Dim match As Match = Regex.Match(txtEmail.Text, pattern)
        If match.Success Then
            'Hash the user's password before entering it into the database.
            Dim pass As String = Crypt.Compute(txtPass.Text, "SHA512", Nothing)

            ' Enter the information from the form into the database.
            Dim sql As String = "INSERT INTO Users(Username, Password, EmailAddress) " & _
                "VALUES(@User, @Pass, @Email)"
            Dim cmd As New SqlCommand(sql, conn)
            cmd.Parameters.AddWithValue("@User", txtName.Text)
            cmd.Parameters.AddWithValue("@Pass", pass)
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text)

            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
        Else
            lblError.Text = "Invalid email address. Please correct."
            lblError.ForeColor = Drawing.Color.Red
        End If

There are more checks that aren't included here that aren't relevant to my problem.

Here's my user login:

            Dim pass As String = Crypt.Compute(txtPass.Text, "SHA512", Nothing)

            Dim UserData As New DataSet
            Dim UserAdapter As New SqlDataAdapter
            UserAdapter.SelectCommand = New SqlCommand("SELECT * FROM Users " & _
                                                       "WHERE Username = @User AND Password = @Pass", conn)
            UserAdapter.SelectCommand.Parameters.AddWithValue("@User", txtUser.Text)
            UserAdapter.SelectCommand.Parameters.AddWithValue("@Pass", pass)
            UserAdapter.Fill(UserData)

            If UserData.Tables(0).Rows.Count <> 1 Then
                lblError.Text = "Invalid username or password."
                lblError.ForeColor = Drawing.Color.Red
                Session("LoginAttempt") = CInt(Session("LoginAttempt")) + 1
            Else
                Session("LoggedIn") = True
                Response.Redirect("Home.aspx")
            End If

As far as I can see, there is no difference in the hashing I've done here.

Does anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月如刀 2024-12-15 08:14:07
  1. 当您通过插入表来创建帐户时,您将使用 txtName.Text 作为用户名,但在检查凭据时,您将使用 txtUser.Text
  2. 为什么使用随机盐?每个加密的盐值是否都必须相同?我已将您的代码粘贴到一个新项目中,当我对同一密码连续运行 Compute 方法两次时,我得到两个不同的结果......显然这是行不通的。尝试传入盐值而不是Nothing,并使用相同的盐来创建帐户和比较登录。以下是一些有效的示例代码:

    将 thePass 调暗为字符串 = "MyPassword"
    将盐调暗为字符串=“盐”
    
    Dim pass As String = Compute(thePass, "SHA512", Encoding.UTF8.GetBytes(theSalt))
    Console.WriteLine(通过)
    Dim pass2 As String = Compute(thePass, "SHA512", Encoding.UTF8.GetBytes(theSalt))
    Console.WriteLine(pass2) 'pass 和 pass2 相同
    

希望这有帮助!

  1. When you creating an account by inserting into the table, you are using txtName.Text for the username, but when checking the credentials you are using txtUser.Text.
  2. Why are you using a random salt? Doesn't the salt have to be the same for every encryption? I've pasted your code into a new project, and when I run the Compute method twice in a row for the same password, I get two different results... obviously that won't work. Try passing in a salt value instead of Nothing, and use the same salt for creating accounts and comparing login. Here's some sample code that works:

    Dim thePass As String = "MyPassword"
    Dim theSalt As String = "salt"
    
    Dim pass As String = Compute(thePass, "SHA512", Encoding.UTF8.GetBytes(theSalt))
    Console.WriteLine(pass)
    Dim pass2 As String = Compute(thePass, "SHA512", Encoding.UTF8.GetBytes(theSalt))
    Console.WriteLine(pass2) 'pass and pass2 are identical
    

Hope this helps!

乄_柒ぐ汐 2024-12-15 08:14:07

除非我错过了(不太熟悉该语言),否则您不会将盐存储在任何地方。

您必须使用在创建验证帐户时使用的相同盐。

附带说明:您可以为每个用户帐户生成随机盐,也可以为所有帐户使用固定盐。任何一种方法都有效。第一个在理论上更安全,但如果盐足够长,则两种方法都可以用于实际目的。

Unless I'm missing it (not really familiar with the language), you don't store the salt anywhere.

You have to use the same salt you've used when creating the account for the verification.

On a side note: You can either generate a random salt for every user account or use a fixed salt for all accounts. Either method works. The first is theoretically more secure, but if the salt is long enough, both are fine for practical purposes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文