2014-10-19 59 views
0

当我有这个简单的MD5加密,但它似乎并没有工作,当我检查我的数据库没有已与我在密码文本框中键入改变了我的密码不会被加密。为什么插入VB.NET

这里是我的代码:

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 
+2

...因为您在*之前插入值*,所以计算散列,然后您不对散列做任何事情。你如何期待数据库接收一个你还没有创建的值? SQL设计得非常好,但它并不具有先见之明。 – David 2014-10-19 14:47:42

+0

你也没有输入密码 - 你是哈希它是一种方式。你不能解密它 – Plutonix 2014-10-19 15:02:12

+0

没有盐的哈希? – Codexer 2014-10-20 00:54:52

回答

2

那是因为你不使用你的任何创造的哈希值。为字符串创建哈希代码不会将字符串更改为哈希代码(即使这样做,您仍然在将字符串发送到数据库后执行此操作)。

计算数据库调用之前的散列码,并创建散列码的字符串表示把字符串:

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

然后使用与散列码而不是字符串从文本字符串:

.Parameters.AddWithValue("@pword, strText)