0

我正在玩关于创建一个包含任何密码组合的纯文本和哈希值的数据库,但是当它们被添加到数据库中时,我最终会得到许多重复项,然后再继续下一个组合......关于为什么的任何想法以及如何阻止它?

<?php
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$numberof=0;
$alphabet = 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");

for($i=0; $i<count($alphabet); $i++){
    for($j=0; $j<count($alphabet); $j++){
        for($k=0; $k<count($alphabet); $k++){
            for($l=0; $l<count($alphabet); $l++){
                $final = $alphabet[$i].$alphabet[$j].$alphabet[$k];
                $hash = md5($final);

                mysqli_query($con,"INSERT INTO hashes (plain, hash) VALUES ('$final', '$hash')");
            }
        }
    }
}
echo $numberof;
?>
4

2 回答 2

1

您有四个嵌套for循环,带有计数器$i $j $k$l. 但是,您在查询中使用的最终字符串仅使用$i $jand$k所以您将有大约 26 个重复项。我假设您打算将值附加$l到字符串的末尾?

于 2013-07-25T12:05:17.943 回答
0

只需删除最后一个循环。我已将您的代码更新如下:

for($i=0; $i<count($alphabet); $i++){
    for($j=0; $j<count($alphabet); $j++){
        for($k=0; $k<count($alphabet); $k++){
            $final = $alphabet[$i].$alphabet[$j].$alphabet[$k];
            $hash = md5($final);
            mysqli_query($con,"INSERT INTO hashes (plain, hash) VALUES ('$final', '$hash')");           
        }
    }
}
于 2013-07-25T12:33:48.193 回答