Checking the MD5 of file in VB.NET

社会主义新天地 提交于 2019-12-13 03:21:03

问题


When a user clicks on the button, it will ask him to choose a specific file. It checks the MD5 hash to know if this is the right file.

The problem with the code is that it gives me "Wrong File" message, and I'm totally sure that the MD5 hash for the file is "3982908442F37245B305EDCF4D834494"

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

        dim md5code as string

        OpenFileDialog1.ShowDialog()

        Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
        Dim f As FileStream = New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
        f = New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
        md5.ComputeHash(f)
        Dim ObjFSO As Object = CreateObject("Scripting.FileSystemObject")
        Dim objFile = ObjFSO.GetFile(OpenFileDialog1.FileName)

        Dim hash As Byte() = md5.Hash
        Dim buff As StringBuilder = New StringBuilder
        Dim hashByte As Byte
        For Each hashByte In hash
            buff.Append(String.Format("{0:X1}", hashByte))
        Next

        md5code = buff.ToString()

        If md5code = "3982908442F37245B305EDCF4D834494" Then
            TextBox2.Text = OpenFileDialog1.FileName
        Else
            MessageBox.Show("Wrong File")
        End If
    End Sub

回答1:


Please see the following MS KB articles:

HashAlgorithm.ComputeHash Method (Stream)

How to compute and compare hash values by using Visual Basic .NET or Visual Basic 2005

Basically, you need to change your generation of the MD5 string to that outlined in either of the articles. To quote the second:

Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
  Dim i As Integer
  Dim sOutput As New StringBuilder(arrInput.Length)
  For i = 0 To arrInput.Length - 1
      sOutput.Append(arrInput(i).ToString("X2"))
  Next
  Return sOutput.ToString()
End Function

You would call this method with md5.Hash as the parameter and store the result in your md5code variable:

md5Code = ByteArrayToString(md5.Hash)



回答2:


Change this: buff.Append(String.Format("{0:X1}", hashByte))

To: buff.Append(String.Format("{0:X2}", hashByte))

You can remove this in the code:

Dim ObjFSO As Object = CreateObject("Scripting.FileSystemObject")  
Dim objFile = ObjFSO.GetFile(OpenFileDialog1.FileName)



回答3:


Paste in the following code for setup:

Imports System.IO
Imports System.Security.Cryptography

Function md5(ByVal file_name As String)
    Dim hash = MD5.Create()
    Dim hashValue() As Byte
    Dim fileStream As FileStream = File.OpenRead(file_name)
    fileStream.Position = 0
    hashValue = hash.ComputeHash(fileStream)
    Dim hash_hex = PrintByteArray(hashValue)
    fileStream.Close()
    Return hash_hex
End Function

Public Function PrintByteArray(ByVal array() As Byte)
    Dim hex_value As String = ""
    Dim i As Integer
    For i = 0 To array.Length - 1
        hex_value += array(i).ToString("X2")
        Next i
        Return hex_value.ToLower
    End Function

When you want to retrieve the MD5 hash, just use md5(file_name) and replace file_name with the path of your file.

For example:

TextBox1.Text = md5("C:\Desktop\foo.txt")


来源:https://stackoverflow.com/questions/7930302/checking-the-md5-of-file-in-vb-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!