CS50破解问题。需要有关如何解密 2-4 个字符的单词的帮助

CS50 crack problem. Need help about how to decrypt 2-4 character words

我是编程新手,这就是我开始做 cs50 的原因。但我被困在 pset2 的破解问题上。正如您在 CS 50 中可能知道的那样,它们给出了最多 4 个字母字符的单词的哈希值,您需要对其进行解密。我已经完成了 1 个字符哈希的解密,但我被困在 2 - 4 个字符的解密中。

目前我只知道基础知识(我在 CS50 的前两周学习的东西)。我从未使用过内存分配和指针。

我的问题是 1.不使用指针是否可以解决这个问题? 2. 我应该如何将 e 参数从我新生成的数组(在这种情况下为 char_2)传递给 crypt 函数,以便在不使用指针的情况下最好不要出现错误。

也许你可以帮助我提供一些关于如何在不使用指针的情况下执行此操作的提示(如果此解决方案需要指针)。

#include <stdio.h>
#include <cs50.h>
#include <crypt.h>
#include <string.h>

int main(int argc, string argv[])
{
if(argc != 2)
{
    printf("Enter the hash code as a single argument\n");
    return 1;
}

string salt = get_string("Imput the salt\n");

string key[] = {
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z", "a",
    "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "y", "z"
};

char temp [40];
string hash = strcpy(temp, argv[1]);


    for(int i=0; i<18; i++)
    {
        string cypher = crypt(key[i], salt);

        int comp = strcmp(cypher, hash);

        if(comp == 0)
        {
            printf("%s\n", key[i]);
            break;
        }

    }
    char char_2[7500];
    for(int i = 0; i < 50; i++)
    {
        for(int j = 0; j < 50; j++)
        {
            sprintf(char_2, "%s%s", key[i], key[j]);
            for(int m = 0; m < strlen(char_2); m++)
            {
                string cypher = crypt(char_2[m], salt);
                int comp = strcmp(cypher, hash);
                if(comp == 0)
                    {
                        printf("%s\n", key[i]);
                        break;
                    }
            }

        }


    }

我收到的错误如下:

crack3.c:69:47: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with &
      [-Werror,-Wint-conversion]
                        string cypher = crypt(char_3[m], salt);

您对 2 个字符的密码的部分方法离工作不远,但主要错误是您尝试将单个密码字符传递给 crypt()(这是行不通的)而不是整个候选密码立刻。将您在内部循环中的代码与此更正进行比较:

            sprintf(char_2, "%s%s", key[i], key[j]);
            // Don't loop over individual letters of password 'char_2' here!
            string cypher = crypt(char_2, salt);
            int comp = strcmp(cypher, hash);
            if (comp == 0)
            {
                printf("%s\n", char_2); // print the found password
                return;                 // no need to look further
            }

当然,您仍然需要为 3、4 和 5 个字符的密码添加代码。