Make Password


The MakePassword application generates passwords from a pool of user supplied letters. The user specifies the number of letters each password can have. The list of passwords is displayed in a listbox and also stored in an array for further processing if required.
This application demonstrates the use of recursive procedures.
    ' A recursive program to calculate the possible permutations
    ' of a password made up of N letters, each from a choice of
    ' M possible choice of letters
    ' The complete list is to be returned in the array m_strPW()
    ' for further use.
    ' P. C.   2004-01-01
    Dim m_lngCount As Long 'pointer to next free member of array
    Dim m_strPW() As String 'array of generated passwords
    Private Sub Generate(ByVal strRoot As String, ByVal strL() As String, ByVal intPWLength As Integer)
        ' A recursive procedure to add intPWLength letters to
        ' strRoot.  If intPWLength is zero, procedur exits.
        If intPWLength > 0 Then  ' add one more letter at a time
            Dim intUp As Integer = strL.GetUpperBound(0)
            Dim intI As Integer
            For intI = 0 To intUp
                Generate(strRoot & strL(intI), strL, intPWLength - 1)
            Next
        Else    'register in final array
            m_strPW(m_lngCount) = strRoot   ' record combination
            m_lngCount += 1     ' increment counter
            'write output for verification
            ListBox1.Items.Add(strRoot)
        End If
    End Sub
    Private Sub MakePW(ByVal strText As String, ByVal intPWLength As Integer)
        ' this procedure generates all possible combinations of
        ' passwords of length intPWLength using the letters
        ' provided in textbox1.text
        ' repetitions are allowed, and assume letters in
        ' textbox1.text have no repetitions
        Dim intLen As Integer = strText.Length()
        Dim strLetters(intLen - 1) As String
        'Note: arrays are zero based
        Dim intI As Integer
        For intI = 0 To strLetters.GetUpperBound(0)
            strLetters(intI) = strText.Substring(intI, 1)
        Next
        ' prepare the password array
        Dim intArrSize As Long = intLen ^ intPWLength
        ReDim m_strPW(intArrSize - 1) 'arrays zero-based
        m_lngCount = 0  'reset counter
        Dim strRoot As String = ""
        Generate(strRoot, strLetters, intPWLength)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strText As String = TextBox1.Text
        Dim intPWLength As Integer = NumericUpDown1.Value
        ListBox1.Items.Clear()  'Clear listbox
        MakePW(strText, intPWLength) ' generate passwords
        ' display number of passwords generated
        ListBox1.Items.Add("No.of PW generated=" & m_lngCount)
    End Sub
Project Files updated 2004-01-01