Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' In button click event check inputdata and call GeneratePassword function
- Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
- ' First we must check that the number in
- ' txtLength.Text property is a real integer number or not
- If Not IsNumeric(txtLength.Text) Then
- MessageBox.Show("Password length must be a whole number")
- txtLength.Focus()
- Exit Sub
- End If
- ' It was an integer so we can keep on
- ' now we check that the pass length is not below 6
- ' because we want the password to be minimum 6 char long
- If CInt(txtLength.Text) < 6 Then
- MessageBox.Show("Password length must be 6 or higher")
- txtLength.Focus()
- Exit Sub
- End If
- ' Check Number of special chars too
- If Not IsNumeric(txtNumSpecialChars.Text) Then
- MessageBox.Show("Num Specialchars must be a whole number")
- txtNumSpecialChars.Focus()
- Exit Sub
- End If
- Dim PassLength As Integer = CInt(txtLength.Text)
- Dim NumSpecialChars As Integer = CInt(txtNumSpecialChars.Text)
- txtPassword.Text = GeneratePassword(PassLength, NumSpecialChars)
- End Sub
- ' Function to return a random generated password
- Public Function GeneratePassword(passwordLength As Integer, Optional numSpecialChars As Integer = 0) As String
- Dim RandomClass As New Random()
- 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"}
- Dim SpecialChars() As String = {"=", "+", "-", "_", "(", ")", "*", "&", "^", "%", "$", "#", "@", "!", "~", "`", "{", "}", "[", "]", ":", "'", "?", "/", ">", ".", ",", "<"}
- Dim FinalPassword As String = ""
- Dim index As Integer = 0
- Dim i As Integer
- If numSpecialChars = 0 Then
- For i = 1 To passwordLength
- index = RandomClass.Next(0, UsableChars.Length() - 1)
- FinalPassword += UsableChars(index)
- Next
- Else
- ' Subtract numSpecialChars from passwordlength
- ' since the password should use special chars also
- passwordLength -= numSpecialChars
- ' run through UsableChars finding random chars for password length
- For i = 1 To passwordLength
- index = RandomClass.Next(0, UsableChars.Length() - 1)
- FinalPassword += UsableChars(index)
- Next
- ' run trough SpecialChars finding random specialchars for numSpecialChars
- For i = 1 To numSpecialChars
- index = RandomClass.Next(0, SpecialChars.Length - 1)
- FinalPassword += SpecialChars(index)
- Next
- ' Next we should shuffle the password so we mix up chars and special chars
- FinalPassword = ShuffleString(FinalPassword)
- End If
- ' Return the shuffled generated password
- Return FinalPassword
- End Function
- Private Function ShuffleString(ByVal strInput As String) As String
- Dim strOutput As String = ""
- Dim rand As New System.Random
- Dim intPlace As Integer
- While strInput.Length > 0
- intPlace = rand.Next(0, strInput.Length)
- strOutput += strInput.Substring(intPlace, 1)
- strInput = strInput.Remove(intPlace, 1)
- End While
- Return strOutput
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement