Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 迭代可能的密码时出现无限循环_C_Cs50 - Fatal编程技术网

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

C 迭代可能的密码时出现无限循环,c,cs50,C,Cs50,我编写了一个公认丑陋的程序,用蛮力破解一个最多5个字母字符的密码,该密码由基于DES的crypt()函数散列,但运行该程序时会导致无限循环。我无法确定原因。有人知道我哪里出错了吗?我只是想问一下无限循环的问题,不过我当然希望得到关于嵌套for循环方法替代方案的建议 #define _XOPEN_SOURCE #include <unistd.h> #include <cs50.h> #include <stdio.h> #include <string.

我编写了一个公认丑陋的程序,用蛮力破解一个最多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;
    }

}
定义源代码
#包括
#包括
#包括
#包括
//函数检查散列的try和用户输入的散列之间是否相等
int check\u password(字符串key\u try,字符串散列);
int main(int argc,字符串argv[])
{
//检查是否有1个命令行参数
如果(argc!=2)
{
printf(“用法:./crack散列”);
返回1;
}
字符串hash=argv[1];
//创建所有字母字符(大写和小写)的数组
字符alpha_字符[52];
对于(int i=0;i<26;i++)
{
字母字符[i]=i+65;
}
对于(int i=0;i<26;i++)
{
字母字符[i+26]=i+97;
}
//散列所有可能的密码,直到try的散列与用户输入的散列匹配
int password_found=0;
字符可能的_密码[6];
while(找到的密码=0)
{
对于(int j=0;j<52;j++)
{
可能的密码[0]=字母字符[j];
可能的_密码[1]='\0';
password\u found=检查密码(可能的密码,散列);
//遍历第二个字符
对于(int k=0;k<52;k++)
{
可能的密码[1]=字母字符[k];
可能的_密码[2]='\0';
password\u found=检查密码(可能的密码,散列);
//遍历第三个字符
对于(int l=0;l<52;l++)
{
可能的密码[2]=字母字符[l];
可能的_密码[3]='\0';
password\u found=检查密码(可能的密码,散列);
//遍历第四个字符
对于(int m=0;m<52;m++)
{
可能的密码[3]=字母字符[m];
可能的_密码[4]='\0';
password\u found=检查密码(可能的密码,散列);
//遍历第五个字符
对于(int n=0;n<52;n++)
{
可能的密码[4]=字母字符[n];
可能的_密码[5]='\0';
password\u found=检查密码(可能的密码,散列);
}
}
}
}
}
}
返回0;
}
//根据参数散列检查可能密码的散列
int检查密码(字符串键,字符串散列)
{
if(strcmp(crypt(key_try,“50”),散列)=0)
{
printf(“%s\n”,key\u try);
返回1;
}
其他的
{
返回0;
}
}

代码终止的唯一方式是最后一次调用
password\u found=check\u password(可能是密码,散列)中的code>返回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++)

代码终止的唯一方式是最后一次调用
password\u found=check\u password(可能的密码,散列)中的code>返回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\u founds==0)
意味着如果程序没有找到密码,它将保持在无限循环中。 在while中有5个for循环,它们都修改了var
password\u founds
,因此如果要检查密码,必须跟踪更改。 我建议您在一个单独的唯一函数中插入这5个检查。

while(password\u founds==0)
意味着如果您的程序没有找到密码,它将保留在无限循环中。 在while中有5个for循环,它们都修改了var
password\u founds
,因此如果要检查密码,必须跟踪更改。
我建议您在一个单独的唯一函数中插入这5个检查。

我没用过这么多。如果我的问题问得不好,我希望你能指导我如何提出更好的问题,而不仅仅是投反对票。你程序中的哪个循环是无限循环的?你对
password\u find
的大多数赋值都是“死的”:在用下一个
check\u password()覆盖它之前,你从不检查值
call。看起来您的
检查密码功能可能有问题,每次调用时它可能返回0。1。是什么让你认为存在无限循环问题?用52个循环嵌套5个循环是52^5或380204032
check_password
调用非常耗时。2.使用调试器,甚至打印语句。我没用过这么多。如果我的问题问得不好,我希望你能指导我如何提出更好的问题,而不仅仅是投反对票。你程序中的哪个循环是无限循环的?你对
password\u find
的大多数赋值都是“死的”:在用下一个
check\u password()覆盖它之前,你从不检查值
call。看起来您的
检查密码功能可能有问题,每次调用时它可能返回0。1。是什么让你认为存在无限循环问题?用52个循环嵌套5个循环是52^5或380204032
check_password
调用非常耗时。2.使用调试器,甚至打印语句。