2012-11-14 38 views
0

我有一個Login form,我還沒有做任何關於hashing的密碼,但我一直在閱讀有關散列,但它真的讓我困惑,並且不知道如何在我的代碼中爲登錄表單實現它。 代碼散列只見vb.net登錄表單的散列密碼

轉換回字節

hashOfBytes = Convert.FromBase64String(strHash) 

**我的登錄表單代碼**

Using conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = forms") 
    Using cmd 
     With cmd 
      MsgBox("Connection Established") 
      .Connection = conn 
      .Parameters.Clear() 
      .CommandText = "SELECT * FROM users WHERE BINARY Username = @iUsername AND Password = @iPassword" 
      .Parameters.Add(New MySqlParameter("@iUsername", txtUser.Text)) 
      .Parameters.Add(New MySqlParameter("@iPassword", txtPass.Text)) 

     End With 
     Try 
      conn.Open() 
      dr = cmd.ExecuteReader() 
     Catch ex As MySqlException 
      MsgBox(ex.Message.ToString()) 
     End Try 
    End Using 
End Using 

If dr.HasRows = 0 Then 

    MsgBox("Invalid user") 
    Conn.Close() 

Else 


    Start.Show() 
    Conn.Close() 


End If 
End Sub 

回答

1

,可以儲存的散列值您的表的密碼字段中的密碼。
然後您搜索用戶和密碼散列,而不是直接從輸入框中獲取密碼。

但是,您的代碼仍然會失敗,因爲您嘗試在處理連接後使用MySqlDataReader。移動使用塊內的行檢查

Dim strHash as string = Convert.ToBase64String(hashOfBytes) 
..... 
Dim userIsValid as Boolean = False 
Using conn As New MySqlConnection(.........) 
Using cmd 
    .... 
     .Parameters.Add(New MySqlParameter("@iPassword", strHashPass)) 
     Try 
      conn.Open() 
      dr = cmd.ExecuteReader() 
      userIsValid = dr.HasRows 
     Catch ex As MySqlException 
      MsgBox(ex.Message.ToString()) 
     End Try 
    End Using 
    End Using 

    if userIsValid then 
     ..... 
    else 
     ..... 
    End