Advertisement
vbtesting51

VB.NET Login using MD5

Oct 17th, 2014
2,876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.09 KB | None | 0 0
  1. Option Strict On
  2. Imports System.IO
  3.  
  4. Public Class Form1
  5.  
  6.     Private Function StringtoMD5(ByRef Content As String) As String
  7.         Dim M5 As New System.Security.Cryptography.MD5CryptoServiceProvider
  8.         Dim ByteString() As Byte = System.Text.Encoding.ASCII.GetBytes(Content)
  9.         ByteString = M5.ComputeHash(ByteString)
  10.         Dim FinalString As String = Nothing
  11.  
  12.         For Each bt As Byte In ByteString
  13.             FinalString &= bt.ToString("x2")
  14.         Next
  15.  
  16.         Return FinalString.ToUpper()
  17.     End Function
  18.  
  19.     Private Function createUser(ByVal user As String, ByVal pass As String) As String ' Declares user and pass as strings
  20.         'check to see if the MD5 has value of user exsists as a text file
  21.         If File.Exists(StringtoMD5(user) & ".txt") = False Then
  22.             Try
  23.                 ' write MD5 hash values of user and pass to a file names <user>.txt
  24.                 My.Computer.FileSystem.WriteAllText(StringtoMD5(user) & ".txt", StringtoMD5(user) & Chr(32) & StringtoMD5(pass), False)
  25.  
  26.                 File.SetAttributes(StringtoMD5(user) & ".txt", FileAttributes.Hidden)
  27.  
  28.                 ' If it works
  29.                 MsgBox("It worked", MsgBoxStyle.Information)
  30.             Catch ex As Exception
  31.                 ' if it fails to write, then show an error message
  32.                 MsgBox("Something had went wrong" & ex.Message, MsgBoxStyle.Information, "Error")
  33.             End Try
  34.         Else
  35.             ' username has already been created
  36.             MsgBox("Username has already been taken")
  37.         End If
  38.  
  39.         ' return values
  40.         Return user
  41.         Return pass
  42.     End Function
  43.  
  44.     Private Sub Ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
  45.         Try
  46.             'check to see if the MD5 hashed value of user.txt and pass.txt are equal to the hash values inside
  47.             'the file. We find the file by using IO.File.ReadAllText to find the MD5 hashed named file of <user>.txt
  48.             'int real situations, you would replace the IO.File.ReadAllText with a connection to your database/server/website
  49.             If StringtoMD5(UsernameTextBox.Text) & Chr(32) & StringtoMD5(PasswordTextBox.Text) = IO.File.ReadAllText(StringtoMD5(UsernameTextBox.Text) & ".txt") Then
  50.                 ' if is works, let the user login
  51.                 ' in this area, you can give a message to the database/server/website to allow the user to connect
  52.                 MsgBox("You have entered the correct credentials", MsgBoxStyle.Information)
  53.             Else
  54.                 ' if credentials did not match
  55.                 MsgBox("You have NOT entered the correct credentials")
  56.             End If
  57.  
  58.         Catch ex As Exception
  59.             'If user never registered or something bizarrly happens
  60.             MsgBox(ex.Message, MsgBoxStyle.Information, "Error")
  61.             Exit Sub
  62.         End Try
  63.  
  64.     End Sub
  65.  
  66.     Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  67.         createUser(UsernameTextBox.Text, PasswordTextBox.Text)
  68.     End Sub
  69.  
  70. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement