Advertisement
Taximaniac

code for random password generation - UPDATED

Jul 26th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.67 KB | None | 0 0
  1. ' In button click event check inputdata and call GeneratePassword function
  2. Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
  3.         ' First we must check that the number in
  4.         ' txtLength.Text property is a real integer number or not
  5.         If Not IsNumeric(txtLength.Text) Then
  6.             MessageBox.Show("Password length must be a whole number")
  7.             txtLength.Focus()
  8.             Exit Sub
  9.         End If
  10.  
  11.         ' It was an integer so we can keep on
  12.         ' now we check that the pass length is not below 6
  13.         ' because we want the password to be minimum 6 char long
  14.         If CInt(txtLength.Text) < 6 Then
  15.             MessageBox.Show("Password length must be 6 or higher")
  16.             txtLength.Focus()
  17.             Exit Sub
  18.         End If
  19.  
  20.         ' Check Number of special chars too
  21.         If Not IsNumeric(txtNumSpecialChars.Text) Then
  22.             MessageBox.Show("Num Specialchars must be a whole number")
  23.             txtNumSpecialChars.Focus()
  24.             Exit Sub
  25.         End If
  26.  
  27.         Dim PassLength As Integer = CInt(txtLength.Text)
  28.         Dim NumSpecialChars As Integer = CInt(txtNumSpecialChars.Text)
  29.  
  30.         txtPassword.Text = GeneratePassword(PassLength, NumSpecialChars)
  31.  
  32.     End Sub
  33.  
  34. ' Function to return a random generated password
  35. Public Function GeneratePassword(passwordLength As Integer, Optional numSpecialChars As Integer = 0) As String
  36.         Dim RandomClass As New Random()
  37.         Dim UsableChars() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  38.         Dim SpecialChars() As String = {"=", "+", "-", "_", "(", ")", "*", "&", "^", "%", "$", "#", "@", "!", "~", "`", "{", "}", "[", "]", ":", "'", "?", "/", ">", ".", ",", "<"}
  39.         Dim FinalPassword As String = ""
  40.         Dim index As Integer = 0
  41.         Dim i As Integer
  42.  
  43.         If numSpecialChars = 0 Then
  44.             For i = 1 To passwordLength
  45.                 index = RandomClass.Next(0, UsableChars.Length() - 1)
  46.                 FinalPassword += UsableChars(index)
  47.             Next
  48.         Else
  49.             ' Subtract numSpecialChars from passwordlength
  50.             ' since the password should use special chars also
  51.             passwordLength -= numSpecialChars
  52.  
  53.             ' run through UsableChars finding random chars for password length
  54.             For i = 1 To passwordLength
  55.                 index = RandomClass.Next(0, UsableChars.Length() - 1)
  56.                 FinalPassword += UsableChars(index)
  57.             Next
  58.  
  59.             ' run trough SpecialChars finding random specialchars for numSpecialChars
  60.             For i = 1 To numSpecialChars
  61.                 index = RandomClass.Next(0, SpecialChars.Length - 1)
  62.                 FinalPassword += SpecialChars(index)
  63.             Next
  64.  
  65.             ' Next we should shuffle the password so we mix up chars and special chars
  66.             FinalPassword = ShuffleString(FinalPassword)
  67.         End If
  68.  
  69.         ' Return the shuffled generated password
  70.         Return FinalPassword
  71.  
  72.     End Function
  73.  
  74.     Private Function ShuffleString(ByVal strInput As String) As String
  75.  
  76.         Dim strOutput As String = ""
  77.         Dim rand As New System.Random
  78.         Dim intPlace As Integer
  79.  
  80.         While strInput.Length > 0
  81.  
  82.             intPlace = rand.Next(0, strInput.Length)
  83.             strOutput += strInput.Substring(intPlace, 1)
  84.             strInput = strInput.Remove(intPlace, 1)
  85.  
  86.         End While
  87.  
  88.         Return strOutput
  89.     End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement