遍历可能的密码时无限循环

Infinite loop when iterating through possible passwords

我写了一个公认丑陋的程序来暴力破解最多 5 个字母字符的密码,该密码由基于 DES 的 crypt() 函数散列,但是当 运行 程序导致无限环形。我无法确定原因。有没有人看到我要去哪里错了?我只是在问无限循环,尽管我当然很感激关于嵌套 for 循环方法的替代方法的建议。

#define _XOPEN_SOURCE
#include <unistd.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>

// function to check for equality between hashed try and user-inputted hash
int check_password(string key_try, string hash);

int main(int argc, string argv[])
{
    // check for 1 command-line argument
    if (argc != 2)
    {
        printf("Usage: ./crack hash");
        return 1;
    }

    string hash = argv[1];

    // create array of all alphabetical characters, upper and lower case
    char alpha_characters[52];
    for (int i = 0; i < 26; i++)
    {
        alpha_characters[i] = i + 65;
    }
    for (int i = 0; i < 26; i++)
    {
        alpha_characters[i + 26] = i + 97;
    }

    // hash all possible passwords until hash of try matches user-inputted hash
    int password_found = 0;
    char possible_password[6];
    while (password_found == 0)
    {
        for (int j = 0; j < 52; j++)
        {
            possible_password[0] = alpha_characters[j];
            possible_password[1] = '[=10=]';
            password_found = check_password(possible_password, hash);
            // iterate through second character
            for (int k = 0; k < 52; k++)
            {
                possible_password[1] = alpha_characters[k];
                possible_password[2] = '[=10=]';
                password_found = check_password(possible_password, hash);
                // iterate through third character
                for (int l = 0; l < 52; l++)
                {
                    possible_password[2] = alpha_characters[l];
                    possible_password[3] = '[=10=]';
                    password_found = check_password(possible_password, hash);
                    // iterate through fourth character
                    for (int m = 0; m < 52; m++)
                    {
                        possible_password[3] = alpha_characters[m];
                        possible_password[4] = '[=10=]';
                        password_found = check_password(possible_password, hash);
                        // iterate through fifth character
                        for (int n = 0; n < 52; n++)
                        {
                            possible_password[4] = alpha_characters[n];
                            possible_password[5] = '[=10=]';
                            password_found = check_password(possible_password, hash);
                        }
                    }
                }
            }

        }

    }
    return 0;
}

// check hash of possible passwords against parameter hash
int check_password(string key_try, string hash)
{
    if (strcmp(crypt(key_try, "50"), hash) == 0)
    {
        printf("%s\n", key_try);
        return 1;

    }
    else
    {
       return 0;
    }

}

代码终止的唯一方式是 lastfor (int n = 0; n < 52; n++) returns 中调用 password_found = check_password(possible_password, hash); 1.


添加5个地方

password_found = check_password(possible_password, hash);
// Add some means to exit the nested loops
if (password_found) return 0;

或...

在 5 个地方

 //       v--- j,k,l,m,n
 for (int x = 0; password_found == 0 && x < 52; x++)

并移除

// while (password_found == 0)

while(password_founds == 0) 意味着如果您的程序没有找到密码,它将保持在无限循环中。 比起你在 while 中有 5 个 for 循环,它们都修改了 varpassword_founds,所以如果你想检查密码,你必须跟踪更改。 我建议您将 5 个支票插入一个单独的唯一函数中。