我正在用 C 语言编写一个程序来破解基于 DES 的加密密码,它将密码作为参数并给我密码。
我所做的是尝试使用相同的盐(前 2 个字母)加密 500000 个单词,然后将其与 argv[1] (这是我想要破解的加密密码)进行比较。我认为这就是所谓的蛮力(尝试一切可能的方法)。 无论如何,我的问题是,当我加密单词时,我得到不同的加密(相同的盐和相同的 key ),正如你所看到的,我打印了数字、单词和加密(只是为了检查它是否有效),如果你愿意,你可以删除它们! p>
顺便说一句,我从某个网站获得了从文件中读取行的代码,因为我是 C 新手,而且我还没有了解文件!
请温柔点,我是新来的:D,如果您对设计或代码有任何评论,请告诉我:)!
顺便说一句,我正在学习 XHarved 的 cs50 类(class),这是黑客版的,所以我不必这样做。这就像额外的作业!
示例:当我在 crypt 函数中加密单词“crimson”时,它会变成 50yoN9fp966dU,但是当我从文件导入它然后加密它时,它会是其他东西(50fy...)。
抱歉问了这么长的问题:|!
如果您愿意,请查看: http://d2o9nyf4hwsci4.cloudfront.net/2014/x/psets/2/hacker2/hacker2.html#_passwords_em_et_cetera_em
#include <stdio.h>
#include <unistd.h>
#include <cs50.h>
#include <string.h>
#define _XOPEN_SOURCE
char *crypt(const char *key, const char *salt);
int main(int argc, char *argv[])
{
static string cryptedText[500000];
static char word[500000][50];
string salt;
int i = 0;
if (argc != 2)
return 1;
FILE *fp;
fp=fopen("wordsTest.txt","r");
if(fp==NULL)
{
printf("Unable to open file.\n");
exit(1);
}
// the first 2 characters are the salt.
salt = strcat(&argv[1][0], &argv[1][1]);
/*crypt every word in wordsTest with the same "salt" and
test if it equals argv[1](crypted pass) */
do
{
if(fgets(word[i],50,fp)!=NULL)
printf("%i ----> %s",i , word[i]);
cryptedText[i] = crypt(word[i], salt);
printf("%s\n", cryptedText[i]);
i++;
}
while (strcmp(cryptedText[i - 1], argv[1]) != 0);
printf ("%s\n", word[i - 1]);
}
我认为 cryptedText 变量不需要是 500000(我可以每次都覆盖它)
最佳答案
正如我的评论中所述:
The input from
fgets()
includes the newline; you don't eliminate it. Therefore, you're comparing the encryption of"crimson"
with"crimson\n"
and the answers must be different.
您在评论中提到:
…for some reason I had to replace the
strcmp
(it didn't compare like it's supposed to) in the while loop tostrncmp
and set the "int n" to 13.
使用一般加密,由于加密数据是一团二进制数据,可能包含嵌入的空字节 '\0'
,您可能会使用 memcmp()
而不是使用 strcmp()
或strncmp()
.
但是,由于您使用的是 crypt()
,输出具有类似于 Base-64 的编码。它生成一个(固定长度)字符串,但您必须复制该字符串才能保留该值。它将被下一次调用crypt()
覆盖。 。因此,您可能正忙于将相同的指针存储到指针数组中。 (旁白:<cs50.h>
header ,特别是它的 string
类型,很烦人!)
旁白:
为了有任何用处,定义如 #define _XOPEN_SOURCE
必须出现在包含任何系统 header 之前。另外,您应该指定您想要的版本号;您可能应该使用 600 或 700,具体取决于您的平台支持的类型。您不必编写 crypt()
的声明您的代码中的函数。出现问题的原因是#define _XOPEN_SOURCE
被放错了地方并且被错误地估价了。
此外,行 salt = strcat(&argv[1][0], &argv[1][1]);
很奇怪并且会调用未定义的行为。这很奇怪,因为它尝试添加从 argv[1]
的第二个字符开始的字符串。到argv[1]
结束,并调用未定义的行为,因为 (a) 字符串重叠,(b) 您正在写入不可用的空间。
关于基于C的DES加密密码的破解功能,不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23436812/