Sky Scraper
Sky Scraper

Reputation: 185

Why does my password doesn't get encrypted when inserted VB.NET

i have this simple md5 encryption but it doesn't seem to be working, when i check my database nothing has changed with what i typed in password textbox.

here's my code:

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

Upvotes: 0

Views: 75

Answers (1)

Guffa
Guffa

Reputation: 700472

That's because you don't use the hash that you created for anything. Creating a hash code for a string doesn't change the string into the hash code (and even if it did, you still do it after sending the string to the database).

Calculate the hash code before the database call, and create a string representation of the hash code to put in the string:

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

Then use the string with the hash code instead of the string from the textbox:

.Parameters.AddWithValue("@pword, strText)

Upvotes: 2

Related Questions