在VB.NET中检查文件的MD5

在VB.NET中检查文件的MD5,vb.net,md5-file,Vb.net,Md5 File,当用户点击按钮时,它会要求用户选择一个特定的文件。它检查MD5散列以确定这是否是正确的文件 代码的问题是它给了我“错误的文件”消息,我完全确定该文件的MD5哈希是“3982908442F37245B305EDCF4D834494” 请参阅以下MS KB文章: 基本上,您需要将MD5字符串的生成更改为这两篇文章中概述的。引用第二段: Private Function ByteArrayToString(ByVal arrInput() As Byte) As String Dim i As

当用户点击按钮时,它会要求用户选择一个特定的文件。它检查MD5散列以确定这是否是正确的文件

代码的问题是它给了我“错误的文件”消息,我完全确定该文件的MD5哈希是“3982908442F37245B305EDCF4D834494”


请参阅以下MS KB文章:

基本上,您需要将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)

请参阅以下MS KB文章:

基本上,您需要将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)

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

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

您可以在代码中删除此项:

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

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

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

您可以在代码中删除此项:

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

粘贴以下代码以进行设置:

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
当您想要检索MD5散列时,只需使用
MD5(文件名)
并用文件路径替换
file\u名

例如:

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

粘贴以下代码以进行设置:

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
当您想要检索MD5散列时,只需使用
MD5(文件名)
并用文件路径替换
file\u名

例如:

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

除了你的代码中有大量垃圾之外,它不应该是
“{0:X2}”
?嗯。。。。你他妈的为什么要把VBS和VB.NET混在一起???这自然会导致问题。。。。一次又一次。除了你的代码中有大量垃圾之外,它不应该是
“{0:X2}”
?嗯。。。。你他妈的为什么要把VBS和VB.NET混在一起???这自然会导致问题。。。。一次又一次。