如何将数据库中的加密密码与登录过程中新输入的密码进行比较



im执行注册和登录表单,当用户在注册阶段输入密码时,我已经对密码进行了加密。因此,要登录,我知道我需要将数据库中的加密密码与登录过程中新输入的加密密码进行比较。我不知道我是否缺少一些代码或写错误的代码。我知道这个问题已经被问到了几次,但我希望我能在这里得到一些帮助。我收到的错误只是无法连接到数据库

的消息

我已经找到了解决方案C#加密登录并尝试遵循代码,但仍然有错误。

           If PasswordTextBox1.Text = "" Or UsernameTextBox2.Text = "" Then
        MessageBox.Show("Please fill-up all fields!", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        'Clear all fields
        PasswordTextBox1.Text = ""
        UsernameTextBox2.Text = ""
        'Focus on Username field
        UsernameTextBox2.Focus()
    Else
        'Connect to DB
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "C:Usersuser1DocumentsVisual Studio 2010ProjectsCryptoCryptocrypto.accdb"
        Try
            'Open Database Connection
            conn.Open()
            Dim sql As String = "SELECT Password FROM registration WHERE Username='" & Encrypt(UsernameTextBox2.Text) & "'"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            Dim sqlRead As OleDbDataReader = cmd.ExecuteReader()
            Dim password As String = cmd.ExecuteScalar().ToString().Replace("", "")
            If (password = Encrypt(PasswordTextBox1.Text)) Then
                PasswordTextBox1.Clear()
                UsernameTextBox2.Clear()
                'Focus on Username field
                UsernameTextBox2.Focus()
                Me.Hide()
                Mainpage.Show()
            Else
                LoginAttempts = LoginAttempts + 1
                If LoginAttempts >= 3 Then
                    End
                Else
                    ' If user enter wrong username or password
                    MessageBox.Show("Sorry, wrong username or password", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    'Clear all fields
                    PasswordTextBox1.Text = ""
                    UsernameTextBox2.Text = ""
                    'Focus on Username field
                    UsernameTextBox2.Focus()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Failed to connect to Database", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'Clear all fields
            PasswordTextBox1.Text = ""
            UsernameTextBox2.Text = ""
        End Try
    End If
End Sub

我希望数据库中的加密密码可以与新输入的密码匹配,以便用户可以登录系统。

这通常发生。当用户注册时,您可以让他们提供用户名和密码,并确认密码。确认的想法是密码通常被掩盖,您要确保他们不会通过保存错字来将自己锁定在帐户中。然后,您可以将提供盐的密码放置,然后用盐和哈希保存用户名。当用户登录时,您将获得该用户的盐并哈希提供的密码,然后将其与存储在数据库中的哈希进行比较。如果哈希匹配,则可以成功验证用户。

哈希被认为比加密更可取,因为它是单向的,因此除了用蛮力之外,没有人可以从哈希(Hash(中逆转密码。盐提供了额外的安全性,因为两个具有相同密码的用户仍然没有相同的哈希。这意味着,如果用户忘记了他的密码,则系统无法将现有密码发送给他,因为它不知道该密码是什么。在这种情况下,用户必须创建一个新密码。如果您使用网站或类似网站的遗忘密码功能,并且他们告诉您当前的密码是什么,则他们使用的是劣等安全性。如果他们使您创建一个新密码,那么他们几乎可以肯定使用哈希。

网络上有很多有关哈希和盐的信息。还值得一提

最新更新