无法从VB.net加密我的MySQL密码作为MD5哈希

时间:2016-01-19 00:02:30

标签: mysql vb.net encryption hash

我正在尝试将用户输入到我的数据库中的密码哈希为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变量放入我的代码中?

2 个答案:

答案 0 :(得分:0)

您的问题的答案基本上与此处的代码相同:

VB.NET login with a MySQL database

直接链接回答:

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

答案 1 :(得分:0)

正如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 & "')"