[英]How to compare the output of crypt() to a cmd line input for cracking passwords in C
我正在使用cs50x入门课程来编写sci课程,其中的一项练习是解密使用crypt(3)函数加密的密码。
他们通过设置以下假设简化了要求:密码长度只能为5个字符,并且每个字符都是大写或小写字母。
我在加密和比较方面遇到了障碍,不确定如何前进。
总结如下。
我已经遍历了AZ和az的所有可能变体,包括A,AA,AAA,AAAA和AAAAA。
我可以将其与argv的用户输入进行比较。
现在,我试图将argv用作哈希并将其与(可能是上面的密码)的哈希进行比较
我在哪里挣扎->
我认为代码应该如何工作
什么不起作用->
存储密码哈希以进行比较,并在操作后使用新哈希更新密码哈希。
我想我明白了->
我想念什么? 我应该继续学习什么?
提前致谢!
您可以将crypt(3)
的结果分配给char*
并从指针引用哈希:
char *compare = crypt(p, salt);
compare = crypt(p, salt);
由于crypt(3)
有其自己的静态数据空间,它将在每次调用时对其进行重写,因此您可能希望使用char数组和strcpy(3)
将哈希值复制到程序的空间中
char hash[256]; // arbitrary length, enough to store the hash without overflowing
char *compare = crypt(p, salt);
strcpy(hash, compare);
或更简洁
char hash[256];
strcpy(hash, crypt(p, salt));
这样,再次调用crypt(3)
时, hash
将不会被覆盖。
请记住在使用crypt(3)
之前先对结果进行null检查,因为它可能在错误时返回null指针。
char *compare = crypt(p, salt);
compare = crypt(p, salt);
if (compare) { // null check
if (strcmp(compare, input) == 0) }
printf("%s is identical to %s!", compare, input);
} else {
printf("%s does not match %s."), compare, input);
}
}
由于您正在使用c字符串,并且我们知道它们是null终止的,因此可以使用strcmp(3)
进行比较,如果您使用的是原始字节,或者不能保证以char结尾的char数组,则最好使用memcmp(3)
进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.