Password Generator

Submitted by:Andery Smith

Date added:01 March, 2011

Category:Visual Basic

Function that generates random passwords.

Tags: generate passwords , passwords generator

Code Snippet:

Dim rNum As New Random(100)
Dim rLowerCase As New Random(500)
Dim rUpperCase As New Random(50)
Dim psw As String
Dim RandomSelect As New Random(50)

Public Function Gen_Psw(ByVal Lenght As Integer, Optional ByVal Reset As Boolean = False) As String
Dim i As Integer
Dim CNT(2) As Integer
Dim Char_Sel(2) As String
Dim iSel As Integer
'clear old passwords
If Reset = True Then
psw = ""
End If
For i = 1 To Lenght
'create random numbers that will represent
'each : upercase,lowercase,numbers

CNT(0) = rNum.Next(48, 57) 'Numbers 1 to 9
CNT(1) = rLowerCase.Next(65, 90) ' Lowercase Characters
CNT(2) = rUpperCase.Next(97, 122) ' Uppercase Characters
'put characters in strings
Char_Sel(0) = System.Convert.ToChar(CNT(0)).ToString
Char_Sel(1) = System.Convert.ToChar(CNT(1)).ToString
Char_Sel(2) = System.Convert.ToChar(CNT(2)).ToString

'pick one of the three above for a character At Random
iSel = RandomSelect.Next(0, 3)
'colect all characters generated through the loop
psw &= Char_Sel(iSel)

' reset with new password
If Reset = True Then

psw.Replace(psw, Char_Sel(iSel))
End If

Return psw

End Function