首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CS50破解问题。我需要关于如何解密2-4个字符的单词的帮助

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

Stack Overflow用户
提问于 2019-08-18 23:03:45
回答 1查看 308关注 0票数 0

我是编程新手,这就是我开始做cs50的原因。但是我被pset2的破解问题困住了。正如你可能知道的,在CS50中,他们给出了一个单词的哈希,最多4个字母字符,你需要解密它。我已经完成了1个字符散列的解密,但我被困在2-4个字符的解密上。

目前,我只知道一些基础知识(我在学习cs50的前两周学到的东西)。我从来没有使用过内存分配和指针。

我的问题是1.有没有可能在不使用指针的情况下解决这个问题? 2.我应该如何将新生成的数组(在这种情况下是char_2)的参数传递给crypt函数,这样错误就不会在不使用指针的情况下出现。

MAybe您可以帮助我在不使用指针的情况下完成此操作(如果此解决方案需要指针)。

代码语言:javascript
复制
#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;
                    }
            }

        }


    }

我收到的错误如下:

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-19 23:31:48

对于2个字符的密码,您的部分方法并不是很有效,但主要的错误是您试图将单个密码字符传递给crypt() (这无法工作),而不是一次传递整个候选密码。将内部循环中的代码与此更正进行比较:

代码语言:javascript
复制
            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个字符的密码添加代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57545767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档