This code sample is a function which generates Random Password. The length of the password is taken as an argument. The password contains only alphanumeric characters.
To generate random passwords, the function uses Rand() function. With a random integer that the function returns every time it is called, we convert it to a valid ASCII alpha-numeric number and append it to the password we have accumulated so far. This is done as many times as the length of the password.
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
Next Return psw
End Function
|
No responses found. Be the first to respond and make money from revenue sharing program.
|