Generating automatic passwords for your users is a common programming scenario. However, due to the techniques typically employed, most autogenerated passwords end up looking like YPSWW9441—which, although highly secure, also end up completely unmemorable.
The following function generates a password using alternating friendly consonants and vowels, making for much more memorable passwords. Asking the function to generate a five-character password, for example, may result in BONES or LAMOT.
To use this function, call GeneratePassword, passing in the length of your desired password. The final password will be returned as a string:
Public Function GeneratePassword(ByVal Length As Integer) As String ' Creates a memorable password of the specified Length Dim blnOnVowel As Boolean Dim strTempLetter As String Dim strPassword As String Dim intCount As Integer For intCount = 1 To Length If blnOnVowel = False Then ' Choose a nice consonant - no C, X, Z, or Q strTempLetter = CType(Choose(CType(GetRandomNumber(1, _ 17), _ Double), _ "B", "D", "F", "G", "H", "J", "K", "L", "M", _ "N", "P", "R", "S", "T", "V", "W", "Y"), String) ' Append it to the password string strPassword += strTempLetter ' Swich to vowel mode blnOnVowel = True Else ' Choose a vowel strTempLetter = CType(Choose(CType(GetRandomNumber(1, 5), _ Double), _ "A", "E", "I", "O", "U"), String) ' Append it to the password string strPassword += strTempLetter ' Switch back again, ready for next loop round blnOnVowel = False End If Next Return strPassword End Function Dim objRandom As New System.Random(CType((System.DateTime.Now. _ Ticks _ Mod System.Int32.MaxValue), Integer)) Public Function GetRandomNumber(Optional ByVal Low As Integer _ = 1, _ Optional ByVal High As Integer = 100) As Integer ' Returns a random number, ' between the optional Low and High parameters Return objRandom.Next(Low, High + 1) End Function
You could use the GeneratePassword function as so:
Dim MyPassword As String MyPassword = GeneratePassword(5) MessageBox.Show(MyPassword)
About the Author
Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2, $49.99), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.