My clients always complain about the random passwords I send them. Even though they have to change the password the first time they log in, they find it difficult to work with the normal random created text that I provide (e.g., zafkpr9b). So, I finally gave in, and created a password generator using a list of words from a text document.
The passwords we will be generating will consist of two words and a number. The words will be loaded from a text document with a list of words. You can find a wordlist using Google. There is a nice English word list at Curlew Communications.
This means we need to do four things:
- Retrieve the list of words
- Generate the two words
- Generate the number
- Add it all together
Retrieve the List of Words
Before I can do anything, I need a list of words. I use the text field parser to loop through all the fields in the text file. This example will work with comma delimited and "word per line" documents.
Dim WordList As New List(Of String)
If My.Computer.FileSystem.FileExists(Filename) Then
Dim fields As String()
Dim delimiter As String = ","
Using parser As New TextFieldParser(Filename)
While Not parser.EndOfData
fields = parser.ReadFields()
For Each word As String In fields
Throw New Exception(Filename & " doesn't Exist!")
Generate the Two Words
To generate the number, I simply call a method that creates a number less than the number of words I retrieved. Next, I select the nth word from the list and use it.
Dim Position As New Random(System.DateTime.Now.Millisecond)
Dim wordnumber As Integer = Position.Next(0, WordList.Count - 1)
Dim word As String = WordList(wordnumber)
Generate the Number
I need a number between 0 and 99 to add to the two words I retrieved above. Therefore, I simply generate a random number between 0 and 99.
Dim RandomClass As New Random(System.DateTime.Now.Millisecond)
Dim rndNumber As Integer = RandomClass.Next(0, 99)
Add it all Together
When adding the words and number together, I first need to know in what order the password should be. To do this, I create another random number and use this number to determine the position of the various words.
Dim PasswordPosition As Integer = RandomClass.Next(0, 5)
Select Case PasswordPosition
Case 0 : Return word1 & word2 & rndNumber
Case 1 : Return rndNumber & word1 & word2
Case 2 : Return word1 & rndNumber & word2
Case 3 : Return word2 & word & rndNumber
Case 4 : Return rndNumber & word2 & word1
Case 5 : Return wood2 & rndNumber & word1
Case Else : Return word1 & word2
I also use the
Mixedcase method that simply returns the word supplied with its first character capitalized.
Private Function Mixedcase(ByVal Word As String) As String
If Word.Length = 0 Then Return Word
If Word.Length = 1 Then Return UCase(Word)
Return Word.Substring(0, 1).ToUpper & Word.Substring(1).ToLower
The Final Result
Instead of receiving a password that makes no sense, we can now supply our clients/users a password that is easy to read and understand. Here are a few passwords that the class generated:
This code can be used to automatically generate a password for users that register on a website or anywhere you need a password that is automatically generated.
- 9th February, 2009: Initial post
- 18th February, 2009: Added The Final Result section.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.