【问题标题】:crypt brute force never ending loopcrypt蛮力永无止境的循环
【发布时间】: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


【解决方案1】:

更改return 中的break 以退出所有循环。

此外,正如 cmets 中指出的那样:

if ( hash == password) 应该 if(!strcmp(hash,password)) 因为你想在 C 中比较两个字符串。

【讨论】:

  • 完美运行,谢谢!只是一个问题,为什么 hash == password 不起作用?它仍在比较两个字符串吗?字符串是否只与 strcmp 函数进行比较?
  • @Tripduc 因为将cs50.h 和typdef-ed char* 编写为string 的恶魔根本没有给你任何好处。 hash == password 所做的只是 C 比较两个 pointers(即每个地址中的地址),而不是它们指向 tocontent。仅仅用言语无法表达隐藏在该头文件中的char* typedef 给该语言的新手带来了多大的挫败感。比较终止字符串的正确方法是 C 等价是通过像 strcmp 这样的函数。
猜你喜欢
  • 1970-01-01
  • 2016-06-04
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-14
相关资源
最近更新 更多