Password combinations

Miaaa72

New member
Joined
Mar 22, 2008
Messages
2
Programming Experience
Beginner
hiiiiz

i have a question... how can i write a program that generates all possible combinations for a number of letters.

like if i had 4 digits... the number of possible combinations = 26^4 right?? but how can i display them all ??
 
There are 10.000 possibilities of 4 digits, 0000 to 9999. For other cases search for "permutations".
 
The counting technique used for number permutations can also be used for any charset. For example A-Z could be considered an alternate number system and any string consisting of these chars a complex number. By implementing a custom counting method you can "count" the string permutations up from first "AAAA" to "ZZZZ". (AAAB, AAAC... AAAZ, AABA, AABB, if you see the pattern)

Setting up a charset:
VB.NET:
Private charset() As Char
Private Sub initCharset()
    Dim tmp As New List(Of Char)
    For i As Integer = 65 To 90 'A to Z
        tmp.Add(Chr(i))
    Next
    charset = tmp.ToArray
End Sub
A counting method that "adds one" to the string value for our custom numerical system:
VB.NET:
Private Function addone(ByVal s As String) As String
    Dim stringChars() As Char = s.ToArray
    For i As Integer = stringChars.Length - 1 To 0 Step -1
        Dim ix As Integer = Array.IndexOf(charset, stringChars(i))
        If ix < charset.Length - 1 Then
            stringChars(i) = charset(ix + 1)
            Return String.Concat(stringChars)
        Else
            stringChars(i) = charset(0)
        End If
    Next
    Return Nothing 'reached max value, can't add more
End Function
Permutation sample, this writes the 26^4 combinations to a file:
VB.NET:
Private Sub permu()
    initCharset()
    Using writer As New IO.StreamWriter("temp.txt")
        Dim s As New String(charset(0), 4) 'initial string AAAA
        Dim count As Integer = 0
        Do Until s = Nothing
            count += 1
            writer.WriteLine(s)
            s = addone(s)
        Loop
        writer.WriteLine("combinations: " & count.ToString)
        MessageBox.Show("Generated " & count.ToString & " permutations.")
    End Using
End Sub
The method used can be applied to many different number systems, like binary, hex, ip-addresses etc.
 
This is the same method, only operating on the charset indexes makes it twice as fast, but this also makes the code more abstract. You have to understand the theory first and have a firm grip about arrays not to slip.
VB.NET:
Private Sub permu2()
    initCharset()              
    Using writer As New IO.StreamWriter("temp.txt")
        Dim count As Integer = 1
        Dim stringCharsIndexes(3) As Integer 'the 4 'letter' array
        Dim s As String = indexesToString(stringCharsIndexes)
        writer.WriteLine(s)
        Do
            For i As Integer = stringCharsIndexes.Length - 1 To 0 Step -1
                If stringCharsIndexes(i) < charset.Length - 1 Then
                    stringCharsIndexes(i) += 1
                    s = indexesToString(stringCharsIndexes)
                    writer.WriteLine(s)
                    count += 1
                    Exit For
                ElseIf i = 0 Then
                    Exit Do
                Else
                    stringCharsIndexes(i) = 0
                End If
            Next
        Loop
        writer.WriteLine("combinations: " & count.ToString)
        MessageBox.Show("Generated " & count.ToString & " permutations.")
    End Using
End Sub

Private Function indexesToString(ByVal idx() As Integer) As String
    Dim tmp(idx.Length - 1) As Char
    For i As Integer = 0 To idx.Length - 1
        tmp(i) = charset(idx(i))
    Next
    Return String.Concat(tmp)
End Function
 
Back
Top