如何在C中比较和解密md5密码哈希值?

时间:2014-05-22 21:06:43

标签: c encryption hash cryptography md5

此程序用于比较密码哈希值。我说它是Reading (filename),但后来我遇到segmentation fault (core dumped)错误。我相信我的main或readfile函数有问题。 fscanf在这里引起了问题吗?在main中for循环的中间参数是什么,我相信它会是行数,对吗?我提出了更好的指导意见。

#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);
    }   
}

3 个答案:

答案 0 :(得分:2)

我看到的一个问题是(可能导致分段错误):

// Extract the salt from the hash
char *salt;
memcpy(salt, &hash[3], 8);
salt[8] = '\0'; 

你不能向salt写任何东西,因为它只是指针, 没有进行内存分配。 如果您知道它的最大大小,例如char salt[16];,则可以在堆栈上声明它。 用法也类似:memcpy(salt, &hash[3], 8);

答案 1 :(得分:1)

分段错误(核心转储)是您在以下情况下获得的错误:

  

通过寻址不存在/已分配的内存。

尝试从非法内存位置读取将导致此错误。即。

  1. 如果你打开一个文件,它会失败,返回的文件指针是NULL,你试图从该文件指针读取。这会给你一个分段错误。

答案 2 :(得分:0)

    dict[i] = string;
    dict = malloc(1000);

这两条线在哪个世界里一起有意义?你设置一个指针(到堆栈分配的字符串!)然后你忽略你的先前缓冲区dict,而不是新的缓冲区。这些指针错误需要修复!