Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Public Class Form1
- '
- ' Basic login form
- ' - Username & Password is saved in " C:\PASSWORD1.txt " (change in code if you like)
- ' - Username & Password is stored like this: USERNAME:PASSWORD_ENCRYPTED
- ' -> Password is encrypted in MD5 (cant decrypt)
- '
- ' 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.
- ' Make sure:
- ' You have textboxes: txtUsername & txtPassword
- ' You have buttons: btnLogin & btnMakeAccount
- Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
- Dim strUsername, strPassword, strSavedPassword As String
- Try
- strUsername = txtUsername.Text.Trim
- strPassword = txtPassword.Text.Trim
- If strUsername = "" Or strPassword = "" Then
- Throw New NullReferenceException("The Username & Password field are required!")
- Else
- strSavedPassword = getPassword(strUsername, "C:\PASSWORD1.txt")
- If (strSavedPassword = strToMD5(strPassword)) Then
- MessageBox.Show("Password correct")
- Else
- MessageBox.Show("Password NOT correct")
- End If
- End If
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- End Try
- End Sub
- Private Function getPassword(ByVal pUsername As String, ByVal Location As String) As String
- Dim srFileReader As System.IO.StreamReader
- Dim sInputLine As String
- Dim Username As String
- Dim i As Integer = 0
- srFileReader = System.IO.File.OpenText(Location)
- sInputLine = srFileReader.ReadLine()
- Do Until sInputLine Is Nothing
- Username = sInputLine.Split(":")(0)
- If (Username = pUsername) Then
- Return sInputLine.Split(":")(1)
- End If
- sInputLine = srFileReader.ReadLine()
- i = i + 1
- Loop
- srFileReader.Close()
- Return Nothing
- End Function
- Private Sub createAccount(ByVal Username As String, ByVal Password As String, ByVal Location As String)
- Dim objWriter As New System.IO.StreamWriter("C:\PASSWORD1.txt", True)
- objWriter.WriteLine(Username & ":" & strToMD5(Password))
- objWriter.Close()
- MessageBox.Show("Account " & Username & " created !")
- End Sub
- Function strToMD5(ByVal strToCrypt As String) As String
- Dim md5Obj As New Security.Cryptography.MD5CryptoServiceProvider
- Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToCrypt)
- bytesToHash = md5Obj.ComputeHash(bytesToHash)
- Dim strResult As String = Nothing
- For Each b As Byte In bytesToHash
- strResult += b.ToString("x2")
- Next
- Return strResult
- End Function
- Private Sub btnMakeAccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMakeAccount.Click
- Dim strUsername, strPassword As String
- strUsername = txtUsername.Text.Trim
- strPassword = txtPassword.Text.Trim
- createAccount(strUsername, strPassword, "C:\PASSWORD1.txt")
- End Sub
- End Class
Add Comment
Please, Sign In to add comment