【发布时间】:2014-04-26 21:26:54
【问题描述】:
我能够显示所选两个不同文件的两个不同 MD5 值,但是,SHA-1 函数为它们显示完全相同的值。这是为什么呢?
我不是一个优秀的程序员,所以我不知道这段代码是否特别好,但非常感谢任何帮助或建议。
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
FileStream file1 = new FileStream(lblBrowse1.Text, FileMode.Open);
FileStream file2 = new FileStream(lblBrowse2.Text, FileMode.Open);
byte[] hash1 = md5.ComputeHash(file1);
byte[] hash2 = md5.ComputeHash(file2);
byte[] hash3 = sha1.ComputeHash(file1);
byte[] hash4 = sha1.ComputeHash(file2);
file1.Close();
file2.Close();
textBox1.Text = BitConverter.ToString(hash1).Replace("-", "");
textBox2.Text = BitConverter.ToString(hash2).Replace("-", "");
textBox6.Text = BitConverter.ToString(hash3).Replace("-", "");
textBox7.Text = BitConverter.ToString(hash4).Replace("-", "");
if (textBox1.Text == textBox2.Text)
{
MessageBox.Show("These two files are identical.");
}
else
{
MessageBox.Show("These two files are different.");
}
【问题讨论】:
-
虽然不太可能,但不能说匹配的哈希等于匹配的数据。哈希确实会发生冲突,因此如果哈希匹配,您应该采取辅助措施来确保匹配数据。
标签: c# hash cryptography md5