用C#证明MD5碰撞 [英] Proving MD5 Collision with C#

查看:93
本文介绍了用C#证明MD5碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在执行以下程序,这些程序将证明包含稍有不同数据的各种文件的MD5冲突.但是,当我使用来自维基百科 http://en.wikipedia.org/wiki/Md5 的可靠碰撞数据创建文件时[ ^ ]我得到的哈希值不匹配,而且都不匹配79054025255fb1a26e4bc422aef54eb4的正确哈希值.但是,使用任何其他文件(通常包含二进制数据),我都可以证明冲突.

这些文件的两个输入是:

Hi, I have the following program working which will prove MD5 collisions for various files containing slightly different data. However, when I create the files using proven collision data from wikipedia http://en.wikipedia.org/wiki/Md5[^]I get hashes that do not match and neither of them match the correct hash of 79054025255fb1a26e4bc422aef54eb4. Yet with any other file (typically containing binary data) I am able to prove collisions.

The two inputs from the files are:

d131dd02c5e6eec4693d9a0698aff95c2fcab58712467eab4004583eb8fb7f89
55ad340609f4b30283e488832571415a085125e8f7cdc99fd91dbdf280373c5b
d8823e3156348f5bae6dacd436c919c6dd53e2b487da03fd02396306d248cda0
e99f33420f577ee8ce54b67080a80d1ec69821bcb6a8839396f9652b6ff72a70







and

d131dd02c5e6eec4693d9a0698aff95c2fcab50712467eab4004583eb8fb7f89
55ad340609f4b30283e4888325f1415a085125e8f7cdc99fd91dbd7280373c5b
d8823e3156348f5bae6dacd436c919c6dd53e23487da03fd02396306d248cda0
e99f33420f577ee8ce54b67080280d1ec69821bcb6a8839396f965ab6ff72a70



我正在使用的程序如下.是否有任何方法可以更改此程序以适应这些十六进制文件,或者我需要编写一个全新的文件-我看到了非常复杂的解决方案,所以我只是想知道是否可以更改下面的程序.谢谢.



The program I''m using is below. Is there any way to alter this program to cater for these hex files or do I need to write a completely new one - I have seen solutions that look extremely complicated so I''m just wondering if the program below can be altered. Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace MD53
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] file1Hash = GetMd5HashOfFile("FileA.txt");
            byte[] file2Hash = GetMd5HashOfFile("FileB.txt");
            ConvertToString(file1Hash);
            ConvertToString(file2Hash);

            byte[] file3Hash = GetSHAOfFile("M13A.txt");
            byte[] file4Hash = GetSHAOfFile("M13B.txt");
            ConvertToString(file3Hash);
            ConvertToString(file4Hash);
            
        }

        static byte[] GetMd5HashOfFile(string filepath)
        {
            using (MD5CryptoServiceProvider hashProvider = new MD5CryptoServiceProvider())
            {
                using (FileStream sr = new FileStream(filepath, FileMode.Open, FileAccess.Read))
                {
                    return hashProvider.ComputeHash(sr);
                }
            }
        }
        static byte[] GetSHAOfFile(string filepath)
        {
            using (SHA1CryptoServiceProvider hashProvider = new SHA1CryptoServiceProvider())
            {
                using (FileStream sr = new FileStream(filepath, FileMode.Open, FileAccess.Read))
                {
                    return hashProvider.ComputeHash(sr);
                }
            }
        }
        static void ConvertToString(byte[] fileHash)
        {
            StringBuilder hex1 = new StringBuilder(fileHash.Length);
            foreach (byte b in fileHash)
                hex1.AppendFormat("{0:x2}", b);
            string output = hex1.ToString();
            Console.WriteLine(output);
            return;
        }
        
    }
}

推荐答案

您的问题是您将数据视为文本-不是.它是十六进制数据,即在Wiki页面上以十六进制数字显示的128字节二进制数据.您将其作为256个文本字符(带有或不带有换行符)进行保存(并读取),这将产生与您期望的完全不同的哈希值.
Your problem is that you are treating the data as text - it isn''t. It is hex data, ie 128 bytes of binary data displayed on the wiki page as hex digits. You are holding it (and reading it) as 256 text characters (with or without line breaks) which will produce completely different hash values from the ones you expect.


这篇关于用C#证明MD5碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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