CS50 Pset2。我的crack.c代码可以编译并运行,但不打印任何内容

时间:2018-12-23 22:22:19

标签: c cs50

到目前为止,我正在研究Pset 2 crack.c,并且已经设法获得了一般概念。但是,我的代码仍然无法正常工作。它可以编译并运行,但不打印任何内容。

我不太确定这是哪里出了问题,我可能正在忽略某些东西?

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

char plaintext[4];
char salt [3];
string alphapool;
string newhash;

int main (int argc, string argv[]) {


   if (argc != 2)
   {
       printf("Error: Input 2 command line arguments\n");
       return 1;
   }

   else

   {
       alphapool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
       salt[0] = argv[1][0]; salt[1] = argv[1][1]; salt[2] = '\0';

       //match a plaintext guess to an array in possibilities (alpha pool)
       //for one letter

       for(int i = 0; i < 26+26; i++)

       { //single letter
           plaintext[0] = alphapool[i];
           plaintext[1] = '\0';

           newhash = crypt(plaintext, salt);

           if ((strncmp(argv[1], newhash,13)) == 0)
           {
               printf("Match found, password is %s",plaintext);
               return true;
           }
       }

       for(int i = 0; i < 26+26; i++)

       {//double letter
          plaintext[0] = alphapool[i];

          for(int j = 0; j < 26+26; j++)
          {
            plaintext[1] = alphapool[i];
            plaintext[2] = '\0';

            newhash = crypt(plaintext, salt);

           if ((strncmp(argv[1], newhash,13)) == 0)
           {
               printf("Match found, password is %s", plaintext);
               return true;
           }

          }
       }

          for(int i = 0; i < 26+26; i++)
       {//three letter
          plaintext[0] = alphapool[i];

          for(int j = 0; j < 26+26; j++)
          {
            plaintext[1] = alphapool[i];

            for (int k = 0; k < 26+26; k++)
            {
                plaintext[2] = alphapool[i];
                plaintext[3] = '\0';
            }

            newhash = crypt(plaintext, salt);

           if ((strncmp(argv[1], newhash,13)) == 0)
           {
               printf("Match found, password is %s", plaintext);
               return true;
           }

       }

   }
            for(int i =0; i < 26+26; i++)
            {
                //four letter word
                  plaintext[0] = alphapool[i];

          for(int j = 0; j < 26+26; j++)
          {
            plaintext[1] = alphapool[i];

            for (int k = 0; k < 26+26; k++)
            {
                plaintext[2] = alphapool[i];

               for (int l = 0; l < 26+26; l++)
               {
                   plaintext[3] = alphapool[i];

               }


            newhash = crypt(plaintext, salt);

           if ((strncmp(argv[1], newhash,13)) == 0)
           {
               printf("Match found, password is %s", plaintext);
               return true;
           }
        }
     }
}

   return 0;

        }
}

我希望生成的纯文本应该是产生与给定哈希匹配的哈希的值。

2 个答案:

答案 0 :(得分:0)

输出的字符串可能已被缓冲,您应使用fflush(参见手册页)从程序返回之前强制将缓冲写入终端。

答案 1 :(得分:0)

当内部循环中应该有alphapool[i]plaintextj时,您正在将k放入l的每个索引中。另外,您需要针对每个密码长度将crypt(以及结果的字符串比较检查)放入 innermost 循环内。而且,您没有将终止'\0'放在密码长度4的plaintext中,但是plaintext也没有足够的空间。

相关问题