Hey guys,
Basically I am making a login thing in visual basic. I can get it to hash the password via MD5 then check it with the database I have etc etc then to login.
However the passwords stored in the database are MD5 + salt. I have the salt but they are different for every user but I can get the salt from the db using
SQL and such.
Problem is I cant get it to work with MD5 + salt. MD5 on its own works fine but with the salt i fail 
Heres the code:
Code:
Imports MySql.Data.MySqlClient
Imports System.Data
Imports MySql.Data
Imports MySql
Imports System.Data.SqlClient
Imports System.Text
Imports System.Security.Cryptography
Public Class LoginForm1
Function getMD5Salt(ByVal strToHash As String) As String
Dim conn As MySqlConnection
conn = New MySqlConnection()
conn.ConnectionString = "server=123.456.789.0;user id=2dlolgiants; password=mmownedisawesome101010; database=2dsdatabaseofawesome"
Try
conn.Open()
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
End Try
Dim strText As String = "SELECT userpass FROM isc_users WHERE userpass='"
Dim salt As String = "SELECT salt FROM users Where username='" & UsernameTextBox.Text
Dim bytHashedData As Byte()
Dim encoder As New UTF8Encoding()
Dim md5Hasher As New MD5CryptoServiceProvider
Dim passwordBytes As Byte() = encoder.GetBytes(strText)
Dim saltBytes As Byte() = encoder.GetBytes(salt)
Dim passwordAndSaltBytes As Byte() = _
New Byte(passwordBytes.Length + saltBytes.Length - 1) {}
For i As Integer = 0 To passwordBytes.Length - 1
passwordAndSaltBytes(i) = passwordBytes(i)
Next
For i As Integer = 0 To saltBytes.Length - 1
passwordAndSaltBytes(i + passwordBytes.Length) = saltBytes(i)
Next
bytHashedData = md5Hasher.ComputeHash(passwordAndSaltBytes)
Dim hashValue As String
hashValue = Convert.ToBase64String(bytHashedData)
Return hashValue
End Function
Function getMD5Hash(ByVal strToHash As String) As String
Dim md5Obj As New MD5CryptoServiceProvider
Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
bytesToHash = md5Obj.ComputeHash(bytesToHash)
Dim strResult As String = ""
For Each b As Byte In bytesToHash
strResult += b.ToString("x2")
Next
Return strResult
End Function
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim conn As MySqlConnection
conn = New MySqlConnection()
conn.ConnectionString = "server=123.456.789.0;user id=2dlolgiants; password=mmownedisawesome101010; database=2dsdatabaseofawesome"
Try
conn.Open()
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
End Try
Dim myAdapter As New MySqlDataAdapter
Dim salt As String = "SELECT salt FROM isc_users Where username='" & UsernameTextBox.Text
Dim sqlquery = "SELECT username, userpass FROM users Where username='" & UsernameTextBox.Text & "' and userpass='" & getMD5Salt(PasswordTextBox.Text) & "'"
Dim myCommand As New MySqlCommand()
myCommand.Connection = conn
myCommand.CommandText = sqlquery
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader()
If myData.HasRows = 0 Then
MessageBox.Show("Invalid Login Details", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Dim frm1 = New Form1
Form1.Show()
Me.Visible = False
End If
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
Private Sub LoginForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub UsernameLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UsernameLabel.Click
End Sub
Private Sub UsernameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UsernameTextBox.TextChanged
End Sub
End Class
Any help at all is appreciated