为什么我的密码不';在插入VB.NET时无法进行加密

为什么我的密码不';在插入VB.NET时无法进行加密,vb.net,encryption,Vb.net,Encryption,我有这个简单的md5加密,但它似乎不起作用,当我检查我的数据库时,我在密码文本框中键入的内容没有任何变化 这是我的密码: Dim strText As String = MetroTextBox6.Text Dim bytHashedData As Byte() Dim encoder As New UTF8Encoding() Dim md5Hasher As New MD5CryptoServiceProvider Using con = new MySqlConnection("serv

我有这个简单的md5加密,但它似乎不起作用,当我检查我的数据库时,我在密码文本框中键入的内容没有任何变化

这是我的密码:

Dim strText As String = MetroTextBox6.Text
Dim bytHashedData As Byte()
Dim encoder As New UTF8Encoding()
Dim md5Hasher As New MD5CryptoServiceProvider

Using con = new MySqlConnection("server = localhost; user id = root; database = db; password = root")
Using cmd = con.CreateCommand()
con.Open()
Dim sqlQuery As String = "INSERT INTO candidate(uname,pword) VALUES("@votes, @pword")

With cmd
    .CommandText = sqlQuery
    .Parameters.AddWithValue("@uname", TextBox4.Text)
    .Parameters.AddWithValue("@pword, MetroTextBox6.Text)

    .ExecuteNonQuery()

 bytHashedData = md5Hasher.ComputeHash(encoder.GetBytes(strText))

End With
MsgBox("Record Inserted")
End Using

这是因为您不使用为任何内容创建的哈希。为字符串创建哈希代码不会将字符串更改为哈希代码(即使更改了,在将字符串发送到数据库后仍会更改)

在数据库调用之前计算哈希代码,并创建哈希代码的字符串表示形式以放入字符串:

bytHashedData = md5Hasher.ComputeHash(encoder.GetBytes(strText))
strText = Convert.ToBase64String(bytHashedData)
然后将字符串与哈希代码一起使用,而不是使用文本框中的字符串:

.Parameters.AddWithValue("@pword, strText)

... 因为在计算散列之前插入值,然后对散列不做任何操作。您希望数据库如何接收您尚未创建的值?SQL设计得很好,但它没有先见之明。此外,您没有加密密码,而是对密码进行哈希运算,这是一种方法。你将无法解密它没有盐的散列?我不知道如何盐。请注意:A)如果你不盐你的密码,你会遇到一些问题,例如有人看你的数据库可以告诉哪些用户有相同的密码。B) MD5对于密码散列是一个糟糕的选择;太快了。C) 首先,你不应该自己实现密码。考虑使用ASP.NET成员资格。MySql对如何做到这一点有自己的看法。