Thursday, August 6, 2009

Snippet: Random Password Generator

A Simple Function for generating Random Password in .NET by passing length of password

Function

Private Function generatePassword(ByVal passwordLength) As String
'Declare variables
Dim sDefaultChars, sMyPassword, iPickedChar As String
Dim iCounter, iDefaultCharactersLength, iPasswordLength As Integer
'Initialize variables
sDefaultChars="abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789"
iPasswordLength = passwordLength
iDefaultCharactersLength = Len(sDefaultChars)
Randomize() 'initialize the random number generator
'Loop for the number of characters password is to have
For iCounter = 1 To iPasswordLength
'Next pick a number from 1 to length of character set
iPickedChar = Int((iDefaultCharactersLength * Rnd()) + 1)
'Next pick a character from the character set using the random number iPickedChar
'and Mid function
sMyPassword = sMyPassword & Mid(sDefaultChars, iPickedChar, 1)
Next
Return sMyPassword
End Function




Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments : on " Snippet: Random Password Generator "