【问题标题】:Unable to verify bcrypt-hashed password from mysql with vb.net无法使用 vb.net 从 mysql 验证 bcrypt-hashed 密码
【发布时间】:2015-03-21 13:33:09
【问题描述】:

我在 vb.net 中有一个使用 mysql 作为数据库的应用程序。该应用程序有一个登录表单。还有一个注册表,可以使用 bcrypt.net 在存档中输入新密码,如本站所示:

http://derekslager.com/blog/posts/2007/10/bcrypt-dotnet-strong-password-hashing-for-dotnet-and-mono.ashx

这是我注册新用户的代码(公共程序Clean 用于清理表单):

 Private Sub btSignUp_Click(sender As Object, e As EventArgs) Handles btSignUp.Click
dim hash as string
        hash = HashPassword(txConfirmPass.Text)
        If (txPass.Text <> txConfirmPass.Text) Then
            MessageBox.Show("Passwords don't matches")
        Else
            Dim sql As String = "INSERT INTO fusion_login(name,user,password) VALUES (@name,@user,@pass)"
            Using myconnection As MySqlConnection = Connection.getInstance.getConnection()
                Using mycommand As New MySqlCommand()
                    With mycommand
                        .CommandText = sql
                        .CommandType = CommandType.Text
                        .Connection = myconnection
                        .Parameters.Add("@name", MySqlDbType.VarChar).Value = txName.Text
                        .Parameters.Add("@user", MySqlDbType.VarChar).Value = txUser.Text
                        .Parameters.Add("@pass", MySqlDbType.VarChar).Value = hash
                    End With
                    Try
                        myconnection.Open()
                        mycommand.ExecuteNonQuery()
                        If (MessageBox.Show("Do you insert a new user again?", "Register", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then 
                            Clean(Me)
                        Else
                            Me.Hide()
                            Login.Show()
                        End If
                    Catch ex As MySqlException
                        MessageBox.Show("Error: " + ex.ToString)
                    Finally
                        myconnection.Close()
                    End Try
                End Using
            End Using
        End If
    End Sub

此代码有效!

现在我正在尝试实现对使用上述过程创建的用户进行身份验证的代码,但我无法让它工作。这是我到目前为止所拥有的:

      Private Sub btLogin_Click(sender As Object, e As EventArgs) Handles btLogin.Click
Dim hash as String
hash = HashPassword(txPass.text)
                Dim sql As String = "SELECT  user,password FROM fusion_login WHERE user = @user AND password = @pass"
                Using myconnection As MySqlConnection = Connection.getInstance.getConnection()
                    Using mycommand As New MySqlCommand()
                        With mycommand
                            .CommandText = sql
                            .CommandType = CommandType.Text
                            .Connection = myconnection
                            .Parameters.Add("@user", MySqlDbType.VarChar).Value = txUser.Text
                            .Parameters.Add("@pass", MySqlDbType.VarChar).Value = hash
                        End With
                        Try
    myconnection.Open()
                        myreader = mycommand.ExecuteReader
                        If myreader.HasRows = 0 Then
                            Me.Hide()
                            FusionPrincipal.Show()
                        Else
                            MessageBox.Show("Error", "Login", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                            txUser.Focus()
                        End If

                        Catch ex As MySqlException
                            MessageBox.Show("Error: " + ex.ToString)
                        Finally
                            myconnection.Close()
                            myreader.Close()
                        End Try
                    End Using
                End Using

我不确定是我输入的密码错误,还是登录时比较用户名/密码错误。这可能是什么问题?

【问题讨论】:

  • 这是一个好的开始——至少选择散列和使用 bcrypt 而不是不太安全的东西,你比大多数人走得更远。不幸的是,您仍然无法为每个用户选择和存储盐值。

标签: mysql vb.net authentication bcrypt.net


【解决方案1】:

问题出在这一行:

If myreader.HasRows = 0 Then

在此代码中,myreader.HasRows 是布尔值,但 0 是整数。要进行此比较,必须首先将 0 值隐式转换为布尔值。将0 转换为布尔值会得到False。这意味着如果数据读取器没有返回任何结果,代码只会进入该If 块。我相信这与你想要发生的相反。相反,只需这样做:

If myreader.HasRows = True Then

或者,甚至更好:

If myreader.HasRows Then

虽然我在这里,但仅仅散列密码是不够的。您还需要为每个用户设置一个盐值,并在创建哈希之前将其与密码结合起来。当您对用户进行身份验证时,您会检索该用户名的盐,将其与提供的密码结合起来,然后将其与结果进行比较。密码比较代码应该更像这样:

Public Function AuthenticateUser(ByVal Username As String, ByVal Password As String) As Boolean
    Dim sql As String = "SELECT  user,password,salt FROM fusion_login WHERE user = @user"
    Using myconnection As MySqlConnection = Connection.getInstance.getConnection(), _
          mycommand As New MySqlCommand(sql, myconnection)

        Try
            mycommand.Parameters.Add("@user", MySqlDbType.VarChar).Value = Username
            myconnection.Open()
            Using rdr As MySqlDataReader = mycommand.ExecuteReader()
                If Not rdr.Read() Then Return False
                Return HashPassword(Password, rdr("salt")).Equals(rdr("password"))
            End Using
        Catch 'You probably want to do more here, but "Return False" was enough for this quick example
             Return False
        End Try
    End Using
End Function

当然,您还需要更新HashPassword() 函数以允许附加参数,您的插入代码首先生成盐,并且您的数据库表有空间来存储值。

还有一件事……即使这也不完全正确。如果你真的想把它做对,你需要确保明文密码只在内存中与SecureString 对象一起保存。

【讨论】:

  • 我已经为上面的站点下载了dll,并在项目中参考使用。在项目标题中:导入 BCrypt.Net.BCrypt。 HashPassword 由本参考提供!!! Bcrypt.HashPassword 生成自动加盐或用户根据本站示例提供!!!!!!
  • 通过实例访问共享成员、常量成员、枚举成员或嵌套类型;将不评估限定表达式。此行中的警告 Return Password.Equals
  • 我需要使用 bcrypt 的功能验证。如何检索存储的哈希密码????
  • 视情况而定。你把它放在哪里了?
  • 我用过火鸟! bcrypt 类有一个布尔函数 verify。 sintax 是验证(普通密码,存储密码)。我想在我的表登录中检索存储的密码!
【解决方案2】:

已经找到了这个案例的解决方案:

If mydatareader.Hasrows Then
   While mydatareader.Read
       If (Bcrypt.Net.Bcrypt.Verify(txtPassword.text, mydatareader.Item("password"))) then
           MsgBox("Valid Credentials")
       else
           MsgBox("Invalid Credentials")
       End if
   End While
  mydatareader.Close()
End If

【讨论】:

    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多