2011-10-28 71 views
0

當用戶點擊按鈕時,它會要求他選擇一個特定的文件。它檢查MD5哈希以確定這是否是正確的文件。在VB.NET中檢查文件的MD5

與代碼的問題是,它給了我「錯誤的文件」的消息,我完全肯定的MD5哈希的文件是「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 
+2

除了 「{0:X2}」'? – samjudson

+0

恩....爲什麼你在混合VBS和VB.NET?這自然會導致問題......一次又一次。 – specializt

回答

1

請看下面的MS KB文章:

HashAlgorithm.ComputeHash Method (Stream)

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

基本上,你需要改變你的generati在任何一篇文章中概述的MD5字符串上。引述第二:

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 

你會調用此方法與md5.Hash作爲參數,並把結果保存在您的md5code變量:

md5Code = ByteArrayToString(md5.Hash) 
0

更改此:buff.Append(String.Format("{0:X1}", hashByte))

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

您可以在編碼中刪除:

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

,以獲得MD5文件,只需粘貼下面的代碼:

'Start Hash Generator 
Function md5(ByVal file_name As String) 
    Return hash_generator("md5", file_name) 
End Function 
Function hash_generator(ByVal hash_type As String, ByVal file_name As String) 
    Try 
     Dim hash 
     If hash_type.ToLower = "md5" Then 
      hash = MD5.Create 
     ElseIf hash_type.ToLower = "sha1" Then 
      hash = SHA1.Create() 
     ElseIf hash_type.ToLower = "sha256" Then 
      hash = SHA256.Create() 
     Else 
      MsgBox("Backend Error: Unknown Hash Type - " & hash_type, MsgBoxStyle.Critical) 
      Return "Error" 
     End If 
     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 
    Catch ex As Exception 
     Return "Error" 
    End Try 
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 
'End Hash Generator 

那麼當你想要得到的MD5,只需使用MD5(FILE_NAME),並與路徑替換FILE_NAME你的文件。

例如:

TextBox1.Text = md5("C:\Desktop\foo.txt") 
從具有在其中的垃圾的負載,而不是它應該是`代碼