我是编程新手,这就是我开始做cs50的原因。但是我被pset2的破解问题困住了。正如你可能知道的,在CS50中,他们给出了一个单词的哈希,最多4个字母字符,你需要解密它。我已经完成了1个字符散列的解密,但我被困在2-4个字符的解密上。
目前,我只知道一些基础知识(我在学习cs50的前两周学到的东西)。我从来没有使用过内存分配和指针。
我的问题是1.有没有可能在不使用指针的情况下解决这个问题? 2.我应该如何将新生成的数组(在这种情况下是char_2)的参数传递给crypt函数,这样错误就不会在不使用指针的情况下出现。
MAybe您可以帮助我在不使用指针的情况下完成此操作(如果此解决方案需要指针)。
#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);发布于 2019-08-19 23:31:48
对于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个字符的密码添加代码。
https://stackoverflow.com/questions/57545767
复制相似问题