【发布时间】:2014-05-02 20:56:07
【问题描述】:
我正在用 C 编写一个程序来破解基于 DES 的加密密码,它将密码作为参数并给我密码。
我所做的是尝试使用相同的盐(前 2 个字母)加密 500000 个单词,然后将其与 argv[1](这是我要破解的加密密码)进行比较。我认为这被称为蛮力(尽一切可能)。 无论如何,我的问题是当我加密单词时,我得到不同的加密(相同的盐和相同的密钥),正如你所看到的,我打印了数字、单词和加密(只是为了检查它是否有效),如果你愿意,你可以删除它们!
顺便说一句,我从某个网站获得了从文件中读取该行的代码,因为我是 C 新手,我还没有了解文件!
请客气,我是新来的 :D,如果你对设计或代码有意见,请告诉我 :)!
顺便说一句,我正在学习 XHarved 的 cs50 课程,这是在黑客版中,所以我不必这样做。这就像额外的家庭作业!
示例:当我在 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(我可以每次都覆盖它)
【问题讨论】:
-
我不确定你在问什么。您有比“我的 DES 蛮力破解器不起作用”更具体的问题吗?
-
我的意思是,当我编译并运行我的程序时,它会从一个名为“wordsTest”的文件中加密 500000 个单词,然后将每个单词与加密密码(带有 argv[1] 的那个)进行比较。我的问题是当它从文件中加密单词时,它给了我不同的加密!
-
fgets()的输入包括换行符;你没有消除它。因此,您将"crimson"的加密与"crimson\n"的加密进行比较,答案肯定不同。 -
Thx @JonathanLeffler !!!,我想这就是我没有阅读 fgets 函数的手册页的原因:P。它现在可以工作了,但由于某种原因,我不得不将 while 循环中的 strcmp(我没有像预期的那样进行比较)替换为 strncmp 并将“int n”设置为 13。无论如何你不应该回答不评论:呸!
标签: c encryption crypt cs50