該程序用於比較密碼哈希值。我得到它說Reading (filename)
,但後來我得到segmentation fault (core dumped)
錯誤。我相信我的main或readfile函數中有些東西是錯誤的。 fscanf在這裏造成問題嗎?主循環for循環中的中間參數是什麼,我相信這是行數,是正確的?我已經提供了更好的方向評論。如何比較和解密C中的md5密碼哈希?
#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不能被解密。 – Kets
變了,是不是更好? –