3

我正在试验一些十六进制扫描仪源。

以下代码有效,但速度很慢:

Public Class frmMain

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadFile.Click

        Dim ArrayHold() As Byte
        Dim Index As Integer = 0
        Dim Str As New StringBuilder
        Dim tStr As String = ""
        Dim tempStr As String = ""
        Dim IndexEnd As Integer = 0
        Dim InputString As String = ""

        OpenDia.Filter = "All Files|*.*"

        If OpenDia.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim myStreamReader As StreamReader = Nothing
            myStreamReader = File.OpenText(OpenDia.FileName)
            InputString = myStreamReader.ReadToEnd()
            ArrayHold = Encoding.Default.GetBytes(InputString)

            Do
                IndexEnd = Index + 9

                For x As Integer = Index To IndexEnd

                    If x > UBound(ArrayHold) Then
                        tempStr = tempStr
                    Else
                        tStr = UCase(Convert.ToString(ArrayHold(x), 16))

                    If tStr.Length < 2 Then tStr = "0" & tStr

                    Str.Append(tStr)
                    tempStr = tempStr & Chr(ArrayHold(x))

                    End If
                Next

                Index = Index + 10
            Loop While IndexEnd < UBound(ArrayHold)

            If InStr(1, Str.ToString, "58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441", vbTextCompare) Then
                Label1.Text = "Eicar-test-signature virus Detected!"
            End If
        End If
    End Sub
End Class

为了加快速度,我可以在 XML 文件中使用这种格式:

?xml version="1.0"?>
<signatures>
  <signature>
    <name>Eicar-Test-Signature</name>
      <hex>58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441</hex>
  </signature>
  <signature>
    <name>Mid/Kakworm-Z</name>
      <hex>66732e4372656174655465787446696c652877642b276b616b2e72656727293b74322e77726974652827524547454449</hex>
  </signature>
</signatures>

但我不知道如何在 VB.NET 中读取和实现 XML 文件。很难吗,有人可以帮忙吗?

4

2 回答 2

4

好的,这是一个如何读取 XML 文件的示例:

Dim xml = <?xml version="1.0"?>
          <signatures>
            <signature>
              <name>Eicar-Test-Signature</name>
              <hex>58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441</hex>
            </signature>
            <signature>
              <name>Mid/Kakworm-Z</name>
              <hex>66732e4372656174655465787446696c652877642b276b616b2e72656727293b74322e77726974652827524547454449</hex>
            </signature>
          </signatures>

Dim dict As New Dictionary(Of String, String)
For Each signature As XElement In xml.Root.Elements
  dict.Add(signature.<name>.Value, signature.<hex>.Value)
Next

XDocument.Load您可能会使用or ,而不是在您的代码中嵌入 XML(如上例所示)XDocument.Parse

于 2013-09-21T17:01:58.293 回答
2

Neolisk 很好地涵盖了 LINQ to XML 部分。这是一种使用 Step 将循环简化为单个 For 循环的方法。此外,它StreamReader在一个Using块中使用,这将确保StreamReader正确关闭和处置。

If OpenDia.ShowDialog = Windows.Forms.DialogResult.OK Then

    Using myStreamReader As StreamReader = File.OpenText(openDia.FileName)

        ArrayHold = Encoding.Default.GetBytes(myStreamReader.ReadToEnd())
    End Using

    Dim arrayLength As Integer = ArrayHold.Length - 1

    For i As Integer = 0 To arrayLength Step 10
        Str.Append(UCase(Convert.ToString(ArrayHold(i), 16).PadLeft(2, "0"c)))
    Next

    If dict.ContainsKey(Str.ToString()) Then
        Label1.Text = dict(str.ToString())
    End If 
End If

StringBuilder在没有额外字符串的情况下使用,并且应该也会稍微提高性能,尽管我打赌你最大的打击是循环遍历你一次构建一个字符的字符串,检查每个字符。

编辑

添加了用于检查字典的代码(基于 Neolisk 将 XML 解析为字典的答案)以确保完整性。

于 2013-09-21T17:13:04.553 回答