尝试将C MD5哈希冲突程序移植到C# [英] Trying to Port a C MD5 Hash-Collision Program to C#

查看:61
本文介绍了尝试将C MD5哈希冲突程序移植到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI我已经编写了一个C程序来检测MD5冲突检测.我习惯于C和指针等,所以程序很小.但是,无论我移植到C#有多少次,它都不起作用.我的C代码是:

HI I have written a C program to detect an MD5 collision detection. I am used to C and pointers etc so the program is quite small. However, no matter how many time I port to C# it doesn''t work. my C code is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <error.h>
#include <openssl/md5.h>

void print_hex(unsigned char *cs, int len)
{
    for (int i = 0; i < len; i++)
        printf("%02x", cs[i]);
    printf("\n");
}

void read_hex_file(char *path, size_t nmemb, unsigned char *buf)
{
    FILE *fd = fopen(path, "r");
    if (!fd) error(EXIT_FAILURE, errno, "read_hex_file()");

    unsigned int x;
    for (int i =  0; i < nmemb; i++) {
        fscanf(fd, "%02x", &x);
        buf[i] = (unsigned char)(0xFF & x);
    }

    fclose(fd);
}

int main(int argc, char *argv[])
{
    if (argc != 3) {
        fprintf(stderr, "Invalid args, %s FILE FILE", argv[0]);
        return EXIT_FAILURE;
    }

    unsigned char input[128], r1[MD5_DIGEST_LENGTH], r2[MD5_DIGEST_LENGTH];

    read_hex_file(argv[1], sizeof(input), input);
    MD5(input, sizeof(input), r1);
    read_hex_file(argv[2], sizeof(input), input);
    MD5(input, sizeof(input), r2);

    print_hex(r1, sizeof(r1));
    print_hex(r2, sizeof(r2));

    if (strncmp((const char *)r1, (const char *)r2, sizeof(r1)) == 0)
        printf("Hashes match\n");
    else
        printf("Hashes differ\n");

    return EXIT_SUCCESS;
}



C#(我想学习)绝对无法解决,所以想知道它是否可能甚至已经在其他地方解决了……我已经看过了.这两个文件包含产生相同哈希值的不同字符串.我在这里从他们那里得到了数据:
http://en.wikipedia.org/wiki/MD5 < ahref target ="_ blank" title =新窗口> ^]

它们会产生相同的哈希值79054025255fb1a26e4bc422aef54eb4

任何帮助欢迎.谢谢.



Am getting absolutely nowhere with C# (which I want to learn) so wondering if it even possible or has it been solved elsewhere... I have looked. The two files contain the different strings which produce the same hash value. I got the data from them here:
http://en.wikipedia.org/wiki/MD5<ahref target="_blank" title="New Window">^]

and they produce the same hash 79054025255fb1a26e4bc422aef54eb4

Any help welcome. Thanks.

推荐答案

您不需要所有这些代码,甚至不需要自己读取文件并传递字节数组.您可以使用ComputeHash方法的Stream变体来做到这一点:
You don''t need all this code, nor do you even need to read the files yourself and pass the byte arrays in. You can just use the Stream variant of the ComputeHash methods to do this:
static void Main(string[] args)
{
    byte[] file1Hash = GetMd5HashOfFile(filepath1);
    byte[] file2Hash = GetMd5HashOfFile(filepath2);
}

static byte[] GetMd5HashOfFile(string filepath)
{
    using(MD5CryptoServiceProvider hashProvider = new MD5CryptoServiceProvider())
    {
        using(FileStream sr = new FileStream(filepath, FileMode.Open, FileAccess.Read))
        {
            return hashProvider.ComputeHash(sr);
        }
    }
}


您不是以相同的方式读取文件.
您的C代码在读取文件时将十六进制字符串转换为字节.
在C#中,您使用不相同的Encoding.UTF8.GetBytes(input).
这是代码:
You are not reading the files the same way.
Your C code converts hex strings to bytes while reading file.
In C# you use Encoding.UTF8.GetBytes(input) which is not the same.
Here is the code:
static void Main(string[] args)
{
    byte[] myBytes1 = ReadHexFile("FileA.txt");
    byte[] myBytes2 = ReadHexFile("FileB.txt");

    using (MD5 md5Hash = MD5.Create())
    {
        string hash1 = GetMd5Hash(md5Hash, myBytes1);
        string hash2 = GetMd5Hash(md5Hash, myBytes2);
        Console.WriteLine("The MD5 hash of " + string.Join("", myBytes1.Select(b => b.ToString("x2"))) + " is: " + hash1 + ".");
        Console.WriteLine("The MD5 hash of " + string.Join("", myBytes1.Select(b => b.ToString("x2"))) + " is: " + hash2 + ".");
    }
    Console.ReadLine();
}

static string GetMd5Hash(MD5 md5Hash, byte[] input)
{
    byte[] data = md5Hash.ComputeHash(input);
    return string.Join("", data.Select(b => b.ToString("x2")));
}

static byte[] ReadHexFile(string path)
{
    System.IO.FileStream fs1 = System.IO.File.OpenRead(path);
    System.IO.StreamReader sr1 = new System.IO.StreamReader(fs1);

    string hexString = sr1.ReadToEnd();
    hexString = System.Text.RegularExpressions.Regex.Replace(hexString, @"\W", "", System.Text.RegularExpressions.RegexOptions.Singleline);
    int ix = 0;

    byte[] myBytes1 = new byte[hexString.Length / 2];
    while (ix < hexString.Length)
    {
        string hexByte = hexString.Substring(ix, 2);
        myBytes1[ix / 2] = byte.Parse(hexByte, System.Globalization.NumberStyles.HexNumber);
        ix += 2;
    }

    return myBytes1;
}


这篇关于尝试将C MD5哈希冲突程序移植到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆