【发布时间】:2017-05-03 08:35:19
【问题描述】:
我一直在这里摸不着头脑,找不到解决方案。 我编写了这段代码是为了破解简单的 4 个字符的密码(见下面的代码)。我可以看到密码已正确生成,并且使用从 A 到 z 的每种字母组合测试了每种可能性,但循环永无止境。有人能告诉我为什么吗?
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <crypt.h>
int main(int argc, string argv[])
{
//check number of arguments
if( argc != 2 )
{
printf("Usage: ./crack hash\n");
}
char str[5];
char salt[] = "..";
strncpy(salt, argv[1], 2);
string hash = argv[1];
string password = "....";
char pass[5];
//brute force loop
for( int i = 65; i < 123; i++)
{
str[0] = i;
for( int j = 65; j < 123; j++)
{
str[1] = j;
for( int k = 65; k < 123; k++)
{
str[2] = k;
for( int l = 65; l < 123; l++)
{
str[3] = l;
str[4] = '\0';
strcpy(pass, str);
password = crypt(pass, salt);
if ( hash == password)
{
printf("%s\n", password);
break;
}
printf("\r%s", pass);
fflush(stdout);
}
}
}
}
}
【问题讨论】:
-
你只打破了第四个循环。其他人继续
-
在尝试所有组合后仍应结束,而不是永无止境。也许这个问题措辞不当。
-
什么是“字符串”类型?
-
hash == password永远不会成为现实。 -
if ( hash == password)应该是if(!strcmp(hash,password))- 但这与循环永远不会结束的原因无关
标签: c brute-force cs50 crypt