PHP brute force password generator

Patrick Lorio picture Patrick Lorio · Aug 11, 2011 · Viewed 14.4k times · Source

I want to be able to input a number and get a password, built from a string or unique characters. So if I have two characters in the string : $string = "AB"; these are the desired results :

-in-|-out-
 0  |  A
 1  |  B
 2  | AA
 3  | AB
 4  | BA
 5  | BB
 6  | AAA
 7  | AAB
 8  | ABA
 9  | ABB
 10 | BBB

And so on. Here is my current code :

for($i = 1; $i < 100; $i++)
{
    echo createString ($i, "AB")."<br/>";
}
function createString ($id, $chars) // THE ISSUE <---
{
    $length = getLength($id, $chars);
    //echo "LENGTH : ".$length."<br/><br/>";
    $string = "";
    for($i = 0; $i < $length; $i++)
    {
        $a = round(($id - 1)/pow($length, $i)); // THE ISSUE <-----
        $local = local($a, strlen($chars));
        $string = $chars{$local - 1}." : ".$string;
    }
    return $string;
}
function local ($num, $max)
{
    $num += $max;
    while($num > $max)
    {
        $num -= $max;
    }
    return $num;
}
/*
 get the length of the output by inputing the "in" and defining the possible characters
*/
function getLength ($id, $chars)
{
    $charNUM = 1;
    $LR = -1;
    $HR = 0;
    while(true)
    {
        $LR = $HR;
        $HR = pow(strlen($chars), $charNUM) + $LR;
        $LR += 1;
        //echo $LR." : ".$HR." : ".$charNUM."<br/>";
        if($id >= $LR && $id <= $HR)
        {
            return $charNUM;
        }
        if($id < $LR)
        {
            return false;
        }
        $charNUM ++;
    }
}

That outputs :

B : 
A : 
A : B : 
B : A : 
B : B : 
A : A : 
A : B : B : 
A : B : A : 
A : A : B : 
A : A : A : 
A : A : B : 
A : B : A : 
A : B : B : 
A : B : A : 
B : A : B : B : 
B : A : B : A : 
B : A : B : B : 
B : A : B : A : 
B : A : A : B : 
B : A : A : A : 
B : A : A : B : 
B : A : A : A : 
B : A : B : B : 
B : A : B : A : 
B : B : B : B : 
B : B : B : A : 
B : B : A : B : 
B : B : A : A : 
B : B : A : B : 
B : B : A : A : 
B : B : A : B : B : 
B : B : A : B : A : 
B : B : A : B : B : 
B : B : A : A : A : 
B : B : A : A : B : 
B : B : A : A : A : 
B : B : A : A : B : 
B : B : A : A : A : 
B : B : B : B : B : 
B : B : B : B : A : 
B : B : B : B : B :

And so on. But it has repeats. I am having issues with the function createString(). I want to access a password somewhere down a brute force password table without having it pre-computed. I don't want a pre-computed array and just access a point of it.

Answer

Nobody moving away from SE picture Nobody moving away from SE · Aug 11, 2011

I will post here the code for translating any positive integer into a system of any given positive integer base (>1) returning the values of each digit.

function convert($number, $base)
{
        $return = array();
        do{
                $return[] = $number % $base;
                $number = floor($number / $base);
        }while($number != 0);
        return $return;
}

So you would call use this function as follows:

function createString($i, $base)
{
        $res = convert($i, strlen($base));
        $str = "";
        foreach($res as $digit)
        {
                $str = $base[$digit] . $str;
        }
        return $str;
}

Try it out. It is formatted a little different from your output but should be readable though.

Some expamle output for the base "AB":

0 -> A    
1 -> B
2 -> BA
3 -> BB
4 -> BAA
5 -> BAB
6 -> BBA
7 -> BBB
8 -> BAAA
9 -> BAAB
10-> BABA
11-> BABB
12-> BBAA
13-> BBAB
14-> BBBA
15-> BBBB