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

标签 c string encryption cs50

我是编程新手,这就是我开始学习 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 字符密码的代码。

关于CS50破解问题。需要有关如何解密 2-4 个字符的单词的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57545767/

相关文章:

c - glutSolidSphere 和 GL_LINE_STRIP 在不同位置绘制但基于相同位置

c - WinAPI - 如何实现ListView排序?

java - 使用分隔符连接字符串并去除空字符串

mysql - MySQL 加密的最佳实践?

c - 使用输入重定向暂停 C 程序

c - 使用 Fork 的递归斐波那契数列(C 语言)

algorithm - 在 Delphi 中快速填充字符串

java - 如何用其他字符替换小写字母?

c - 异或加密失败

c++ - 尝试从 Visual Studios C++ 中的加密字符串输出解密字符串时出现逻辑错误