65.9K
CodeProject is changing. Read more.
Home

Text Based Random Password Generator

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (10 votes)

Feb 9, 2009

CPOL

2 min read

viewsIcon

46177

downloadIcon

2010

An article that demonstrates how to use a list of words to generate a user-friendly random password.

Introduction

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 Problem

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)
        parser.SetDelimiters(delimiter)
        While Not parser.EndOfData
            fields = parser.ReadFields()
            For Each word As String In fields
                WordList.Add(word)
            Next
        End While
    End Using

Else
    Throw New Exception(Filename & " doesn't Exist!")
End If

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)
Return word

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
End Select

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
End Function

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:

  • Orange28Wattmeter
  • 23GreenWaitress
  • AardvarkRed5

Conclusion

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.

History

  • 9th February, 2009: Initial post
  • 18th February, 2009: Added The Final Result section.