Advertisement
Taximaniac

code for random password generation

Jul 26th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.76 KB | None | 0 0
  1.     Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
  2.         ' First we must check that the number in
  3.         ' txtLength.Text property is a real integer number or not
  4.         If Not IsNumeric(txtLength.Text) Then
  5.             MessageBox.Show("Password length must be a whole nmber")
  6.             txtLength.Focus()
  7.             Exit Sub
  8.         End If
  9.  
  10.         ' It was an integer so we can keep on
  11.         ' now we check that the pass length is not below 6
  12.         ' because we want the password to be minimum 6 char long
  13.         If CInt(txtLength.Text) < 6 Then
  14.             MessageBox.Show("Password length must be 6 or higher")
  15.             txtLength.Focus()
  16.             Exit Sub
  17.         End If
  18.  
  19.         ' okay, so the length is now a number and is 6 or more char lonng
  20.         ' so we can begin the password generation
  21.  
  22.         ' setup som variables.
  23.         Dim RandomClass As New Random()
  24.         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"}
  25.         Dim SpecialChars() As String = {"=", "+", "-", "_", "(", ")", "*", "&", "^", "%", "$", "#", "@", "!", "~", "`", "{", "}", "[", "]", ":", "'", "?", "/", ">", ".", ",", "<"}
  26.         Dim FinalPassword As String = ""
  27.         Dim index As Integer = 0
  28.         Dim PassLength = CInt(txtLength.Text)
  29.         Dim i As Integer
  30.  
  31.         For i = 1 To PassLength
  32.             index = RandomClass.Next(0, UsableChars.Length() - 1)
  33.             FinalPassword += UsableChars(index)
  34.         Next
  35.  
  36.         txtPassword.Text = FinalPassword
  37.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement