【问题标题】:Having trouble encrypting my MySQL password as MD5 hash from VB.net无法将我的 MySQL 密码加密为来自 VB.net 的 MD5 哈希
【发布时间】:2016-04-24 07:25:16
【问题描述】:

我正在尝试将用户输入我的数据库的密码作为 MD5 进行哈希处理,但我遇到了麻烦。我知道 MD5 不像以前那么安全了,现在也没有加盐了,这只是为了测试目的,我绝不会实际部署它以供人们使用。这只是为了好玩!用户名被输入到数据库中,但密码没有。这是我的代码:

Imports MySql.Data.MySqlClient
Imports System.Security.Cryptography
Imports System.Text


Public Class frmSignup
Dim ServerString As String = "Server=localhost;User        Id=root;Password=;Database=accountinfo"
Dim SQLConnection As MySqlConnection = New MySqlConnection

Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    SQLConnection.ConnectionString = ServerString

    Try
        If SQLConnection.State = ConnectionState.Closed Then
            SQLConnection.Open()
            MsgBox("Successfully connected to DB")

        Else
            SQLConnection.Close()
            MsgBox("Failed to connect to DB")
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)

    End Try
End Sub

Public Sub SaveAccountInformation(ByRef SQLStatement As String)
    Dim cmd As MySqlCommand = New MySqlCommand

    With cmd
        .CommandText = SQLStatement
        .CommandType = CommandType.Text
        .Connection = SQLConnection
        .ExecuteNonQuery()
    End With
    SQLConnection.Close()
    SQLConnection.Dispose()
End Sub

Private Sub btnSignup_Click(sender As Object, e As EventArgs) Handles btnSignup.Click
    If txtPasswd.Text = txtPasswd2.Text Then
        MessageBox.Show("Passwords Match!")

        Dim HashedPass As String = ""

        'Converts the Password into bytes, computes the hash of those bytes, and then converts them into a Base64 string

        Using MD5hash As MD5 = MD5.Create()

            System.Convert.ToBase64String(MD5hash.ComputeHash(System.Text.Encoding.ASCII.GetBytes(txtPasswd.Text)))

        End Using


        Dim SQLStatement As String = "INSERT INTO accountinfodb(`Usernames`, `Passwords`) VALUES ('" & txtUsername.Text & "','" & HashedPass & "')"
        SaveAccountInformation(SQLStatement)



        MessageBox.Show("Account Successfully Registered")
        frmLogin.Show()
        frmLoginScreen.Hide()
    Else
        MessageBox.Show("Passwords Do Not Match!")
        txtPasswd.Text = Focus()
        txtPasswd.Clear()
        txtPasswd2.Text = Focus()
        txtPasswd2.Clear()

    End If
End Sub
End Class

我想我可能在我的 SQL 查询中添加了错误的值,但是如果我添加了 txtPasswd,我不确定将 HashedPass 变量放在我的代码中的什么位置?

【问题讨论】:

标签: mysql vb.net encryption hash


【解决方案1】:

你的问题的答案在这里找到了基本相同的代码:

VB.NET login with a MySQL database

回答的直接链接:

https://stackoverflow.com/a/22939770/1475285

【讨论】:

  • 那是我使用的资源是的,但他的 SQL 查询不同,我试着玩弄它,但它只是把其他一切都搞砸了。有没有办法用我的 SQL 查询来代替?
  • 基本上你的 HashedPass 变量没有值,你想存储这个函数的结果 System.Convert.ToBase64String(MD5hash.ComputeHash(System.Text.Encoding.ASCII.GetBytes(txtPasswd.Text)) ) 在 HashedPass 中
【解决方案2】:

正如 Bread102 所述,您没有将散列函数结果分配给您的变量。以下应该适用于您的情况

Dim HashedPass As String = ""
Using MD5hash As MD5 = MD5.Create()
    HashedPass = System.Convert.ToBase64String(MD5hash.ComputeHashSystem.Text.Encoding.ASCII.GetBytes(txtUsername.Text)))
End Using


Dim SQLStatement As String = "INSERT INTO accountinfodb(`Usernames`, `Passwords`) VALUES ('" & txtUsername.Text & "','" & HashedPass & "')"

【讨论】:

  • 谢谢,您的代码有效,但是当我登录时,它不成功,可能是因为它将MD5哈希与登录表单中的密码进行比较,显然彼此不相等,我该如何过去这?事实上,在将我的哈希与另一个线程进行比较后,它们看起来都非常不同,这是因为哈希没有加盐吗?
  • 据我了解,您应该对输入登录表单的密码进行哈希处理,然后将此哈希与您存储的哈希进行比较以验证它们是否正确。
猜你喜欢
  • 1970-01-01
  • 2011-02-11
  • 2020-09-16
  • 2016-10-29
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多