Click here to Skip to main content
Click here to Skip to main content

Text Based Random Password Generator

, 18 Feb 2009
Rate this:
Please Sign up or sign in to vote.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sean Botha
Web Developer
South Africa South Africa
No Biography provided

Comments and Discussions

 
GeneralCOMPARING DATA IN LISTBOX PinmemberJimoh Ajibola Akinyemi10-Mar-09 8:24 
GeneralRe: COMPARING DATA IN LISTBOX PinmemberSean Botha11-Mar-09 0:36 
Generalgood idea PinmemberDonsw4-Mar-09 9:37 
GeneralNice PinmemberSmirkinGherkin19-Feb-09 2:06 
GeneralRe: Nice PinmemberSean Botha19-Feb-09 2:33 
Generaljust for the beauty for your article PinmemberJarno Burger18-Feb-09 2:35 
could you put some samples of what the results are with your code ?
 
Jarno Burger
Video Jockey

GeneralRe: just for the beauty for your article PinmemberSean Botha18-Feb-09 20:07 
GeneralRe: just for the beauty for your article PinmemberJarno Burger23-Feb-09 5:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 18 Feb 2009
Article Copyright 2009 by Sean Botha
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid