【发布时间】:2014-07-12 02:17:14
【问题描述】:
该程序用于比较密码哈希。我说的是Reading (filename),但后来我收到segmentation fault (core dumped) 错误。我相信我的 main 或 readfile 函数有问题。 fscanf 是否在这里引起了问题? main 中 for 循环中的中间参数是什么,我相信它是行数,对吗?我已经为更好的方向提供了 cmets。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "crypt.h"
int tryguess(char *hash, char *guess)
{
// Extract the salt from the hash
char *salt;
memcpy(salt, &hash[3], 8);
salt[8] = '\0';
// Hash the guess using the salt
char *hashGuess = md5crypt(guess, salt);
// Compare the two hashes
if (strcmp(hashGuess, hash) == 0)
{
return 1;
}
else
{
return 0;
}
}
// Given a hash and a dictionary of guesses,
// try all guesses and return the matching guess.
char *crack(char *hash, char *dict[])
{
int i = 0;
while (dict[i])
{
if (tryguess(hash, dict[i])) return dict[i];
i++;
}
return NULL;
}
// Read in a file.
// The first line of the file is the number of lines in the file.
// Returns an array of strings, with the last element being a NULL
// indicating the end of the array.
char **read_file(char *fname)
{
char **dict;
printf("Reading %s\n", fname);
FILE *d = fopen(fname, "r");
if (! d) return NULL;
// Get the number of lines in the file
char *size;
fscanf(d, "%s[^\n]", size);
int filesize = atoi(size);
// Allocate memory for the array of strings (character pointers)
dict[0] = malloc(100 * sizeof(char *));
// Read in the rest of the file, allocting memory for each string
// as we go.
int count = 0;
int index = 0;
while (count < filesize)
{
for (int i = 0; dict[i] != NULL; i++)
{
fscanf(d, "%s[^\n]\n", dict[i]);
if (dict[i+1] != NULL)
{
dict[i+1] = malloc(1000);
}
count++;
index++;
}
}
// NULL termination. Last entry in the array should be NULL.
dict[index] = NULL;
printf("Done\n");
fclose(d);
return dict;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s hash_file dict_file\n", argv[0]);
exit(1);
}
char **dictionary = read_file(argv[2]);
char **hashes = read_file(argv[1]);
// For each hash, try every entry in the dictionary.
// Print the matching dictionary entry.
for (int i = 0; i < (# of lines); i++)
{
char *hash = hashes[i];
char *result = crack(hash, dictionary);
printf("%s", result);
}
}
【问题讨论】:
-
md5 是一个哈希值。它不能被“解密”。你要求用汉堡包做一头牛。 md5是绞肉机。
-
如果要解密,请查看彩虹表,因为MD5无法解密。
-
变了,好些了吗?
-
@MarcB 是的,但是当然,如果您想复制特定的汉堡包,则可以繁殖和研磨许多不同的奶牛,直到找到与目标汉堡包相同的汉堡包为止。这是 OP 考虑代码的意图,所以您唯一的一点是术语之一。
-
你无法解密它,你能做的是找到返回相同
MD5哈希码的字符串(蛮力)。以下是一些有用的信息:Are there two known strings which have the same MD5 hash value?
标签: c encryption hash cryptography md5