迭代可能的密码时无限循环

问题描述 投票:0回答:2

我写了一个公认的丑陋程序,用暴力破解最多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] = '\0';
            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] = '\0';
                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] = '\0';
                    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] = '\0';
                        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] = '\0';
                            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;
    }

}
c cs50
2个回答
3
投票

代码终止的唯一方法是,如果password_found = check_password(possible_password, hash);中对for (int n = 0; n < 52; n++)的最后一次调用返回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)

1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.