Generate a Random Password - November 2009

Ten years ago I wrote a class module to generate a password. It was written in VB6. Click here to view that tip. I recently updated the code and converted it to VS 2008 VB.Net. So it becomes the November 2009 Tip-of-the-Month.

The Demo Program

 I wrote a web project to demonstrate the tip:

The generated password is shown in the first column. The encoded password is shown in the second column. The decoded, encoded password is shown in the third column. The options used to create the password are shown in the next columns. For example: The first password was created using lower case characters only.

Class cGenPassword

Call class cGenPassword to generate the password. Code similar to this is used in the demo:

Private Sub DoGenerate()
   
Dim oGP As New cGenPassword
    Dim oED As New cEncodeDecode
    For i As Integer = 1 To 12   'generate some passwords
        Dim sPassword As String = oGP.GeneratePassword()
        Dim sPasswordED As String = oED.EncodeDecode(sPassword)
        Dim sPasswordDE As String = oED.EncodeDecode(sPassword, False)
        AddRow(i.ToString, sPassword, sPasswordED,
sPasswordDE, False)
   
Next
End Sub

cGenPassword Methods

Method Parameters Description
GeneratePassword   Returns a randomly generated password 8 characters long and in all lower case.
This is the same as specifying: GeneratePassword(8, False, True, False, False)
GeneratePassword iLength Returns a password of iLength characters. iLength must be specified. Upper and lower case characters are returned. Numbers and special characters are not returned. This is the same as specifying for a 6 character password:
GeneratePassword(6, True, True, False, False)
GeneratePassword iLength,
bUseUpperCase,
bUseLowerCase
[,bUseNumbers=False][,bUseSpecialCharacters=False]
Returns a password of iLength characters. iLength, bUseUpperCase and bUseLowerCase must be specified. bUseNumbers and bUseSpecialCharacters are optional and default to False.
The maximum value for iLength is the combined length of the strings from which the password is being generated - the 'pick from' string. For example: If only numbers are being used then the maximum would be the length of the number string which is 8. If iLength is greater than the length of the 'pick from' string iLength is set to the length of the 'pick from' string.
Select From Upper Case "ABCDEFGHJKMNPQRSTUVWXYZ"
  Lower Case "abcdefhjkmnprstuvwxyz"
  Numbers "23456789"
  Special Characters "#@%$+"
Upper case letters I, O and L are excluded. Lower case letters i, o, l, g and q are excluded. Numbers 0 and 1 are excluded. These characters and numbers are not used because they can be confused with other characters. For example: It is sometimes difficult to distinguish the number '0' from characters 'o' or 'O'.

If bUseUpperCase, bUseLowerCase, bUseNumbers, and bUseSpecialCharacters are all False then all are assumed to be True. In other words use all letters, number, characters if none are specified.

No character is used more than once in the password. For example: The letter "H" will only appear once in the generated password.

Encode and Decode

The EncodeDecode method from class cEncodeDecode.vb is used to encode and decode the password. That module is included with the downloadable source code. EncodeDecode is based upon a VB6 version. Click here to view the documentation for the VB6 version of EncodeDecode.

Other Modules

Modules modEmpty.vb and modDumpnbsp.vb are also included. Functions defined in those modules are the subject of other tips. Click here to view the modEmpty documentation. Click here to view the modDumpnbsp functions. Both modules are included with the downloadable source code.

Download VB.Net Code

Download - VS2008 Zipped File

 Size 

Generate Password GeneratePassword.zip 29k

Previous Tip   Tip Index    Next Tip

Home

Updated 2011/04/16