How to build a character table
$chars = array
(
' ',
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
':', ';', '<', '=', '>', '?', '`',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'{', '|', '}', '~'
);
With the characters from the $chars
array, I would like to find all possible combinations, for a length up to $n
.
**For Example**:
It should start off with ' ', and then go to '!'.
Once it gets to the end of the $chars array (`~`) it should add on another charter.
Run th开发者_运维知识库ough those combinations ('! ', '" ', ... '~ ', ' !' ... '~~', ' ', ect).
And then just keep on going ...
http://eternalrise.com/blog/brute-force-php-script/
If You want to find just a number of possible combinations, it would be combination - permutation math - You have to raise your length $n to a power of elements in Your array. In Php that would be:
echo pow($n, count($chars));
where $n - your combination length.
Math reference: combinations - permutations
P.S. Salute Zackmans solution, but I wonder if it (and any others in that matter) doesn't cause PHP script timeout because of the scope of the problem.
This function will take an array $permutation
only containing elements from another array $set
and generate the next permutation up to a maximum length of $max
.
function next_permutation($permutation, $set, $max)
{
if (array_unique($permutation) === array(end($set)))
{
if (count($permutation) === $max)
{
return FALSE;
}
return array_fill(0, count($permutation) + 1, $set[0]);
}
foreach (array_reverse($permutation, TRUE) as $key => $value)
{
if ($value !== end($set))
{
$permutation[$key] = $set[array_search($value, $set) + 1];
break;
}
$permutation[$key] = $set[0];
}
return $permutation;
}
The function could then be used in a way like this, to iterate over each possible permutation and check it against a hashed password.
$set = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
$max = 3;
$permutation = array($set[0]);
$password = '403926033d001b5279df37cbbe5287b7c7c267fa';
do {
$string = implode('', $permutation);
if (sha1($string) === $password)
{
echo 'Password found: "'.$string.'"';
break;
}
} while ($permutation = next_permutation($permutation, $set, $max));
This would print Password found: "lol"
精彩评论