Guest User

Untitled

a guest
Jan 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. Imports System.IO
  2.  
  3. Public Class Form1
  4. '
  5. ' Basic login form
  6. ' - Username & Password is saved in " C:\PASSWORD1.txt " (change in code if you like)
  7. ' - Username & Password is stored like this: USERNAME:PASSWORD_ENCRYPTED
  8. ' -> Password is encrypted in MD5 (cant decrypt)
  9. '
  10. ' You can add as many accounts as you want, if you create an account with a username which is already in use, it will be ignored.
  11.  
  12.  
  13. ' Make sure:
  14. ' You have textboxes: txtUsername & txtPassword
  15. ' You have buttons: btnLogin & btnMakeAccount
  16.  
  17.  
  18. Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
  19. Dim strUsername, strPassword, strSavedPassword As String
  20. Try
  21. strUsername = txtUsername.Text.Trim
  22. strPassword = txtPassword.Text.Trim
  23.  
  24. If strUsername = "" Or strPassword = "" Then
  25. Throw New NullReferenceException("The Username & Password field are required!")
  26. Else
  27. strSavedPassword = getPassword(strUsername, "C:\PASSWORD1.txt")
  28. If (strSavedPassword = strToMD5(strPassword)) Then
  29. MessageBox.Show("Password correct")
  30. Else
  31. MessageBox.Show("Password NOT correct")
  32. End If
  33. End If
  34. Catch ex As Exception
  35. MessageBox.Show(ex.Message)
  36. End Try
  37. End Sub
  38. Private Function getPassword(ByVal pUsername As String, ByVal Location As String) As String
  39. Dim srFileReader As System.IO.StreamReader
  40. Dim sInputLine As String
  41. Dim Username As String
  42. Dim i As Integer = 0
  43. srFileReader = System.IO.File.OpenText(Location)
  44. sInputLine = srFileReader.ReadLine()
  45. Do Until sInputLine Is Nothing
  46. Username = sInputLine.Split(":")(0)
  47. If (Username = pUsername) Then
  48. Return sInputLine.Split(":")(1)
  49. End If
  50.  
  51. sInputLine = srFileReader.ReadLine()
  52. i = i + 1
  53. Loop
  54. srFileReader.Close()
  55. Return Nothing
  56. End Function
  57. Private Sub createAccount(ByVal Username As String, ByVal Password As String, ByVal Location As String)
  58. Dim objWriter As New System.IO.StreamWriter("C:\PASSWORD1.txt", True)
  59. objWriter.WriteLine(Username & ":" & strToMD5(Password))
  60.  
  61. objWriter.Close()
  62. MessageBox.Show("Account " & Username & " created !")
  63. End Sub
  64. Function strToMD5(ByVal strToCrypt As String) As String
  65. Dim md5Obj As New Security.Cryptography.MD5CryptoServiceProvider
  66. Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToCrypt)
  67. bytesToHash = md5Obj.ComputeHash(bytesToHash)
  68. Dim strResult As String = Nothing
  69. For Each b As Byte In bytesToHash
  70. strResult += b.ToString("x2")
  71. Next
  72. Return strResult
  73. End Function
  74.  
  75. Private Sub btnMakeAccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMakeAccount.Click
  76. Dim strUsername, strPassword As String
  77. strUsername = txtUsername.Text.Trim
  78. strPassword = txtPassword.Text.Trim
  79. createAccount(strUsername, strPassword, "C:\PASSWORD1.txt")
  80. End Sub
  81.  
  82.  
  83. End Class
Add Comment
Please, Sign In to add comment