-1

在终于解决了我的最后一个问题之后,我结束的代码是

Function MD5(ByVal strToHash As String) As String
    Dim md5Obj As New Security.Cryptography.MD5CryptoServiceProvider
    Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)

    bytesToHash = md5Obj.ComputeHash(bytesToHash)

    Dim strResult As String = ""

    For Each b As Byte In bytesToHash
        strResult += b.ToString("x2")
    Next

    Return strResult
End Function



Dim words As IEnumerable(Of String) = File.ReadLines(OpenFileDialog1.FileName)
    For Each word As String In words
        If String.Equals(MD5(word), hash.Text) Then
            Label2.Text = word
        Else : Label2.Text = "Hash Could Not Be Cracked"
        End If
    Next

现在,一旦散列字与我输入的散列匹配,我需要让它停止!

4

2 回答 2

0

您可以使用 LINQ's FirstOrDefault,因为您正在使用ReadLines而不是ReadAllLines

Dim firstWord = (From line In IO.File.ReadLines(OpenFileDialog1.FileName)
    Where String.Equals(MD5(line), hash.Text)).FirstOrDefault()
If firstWord IsNot Nothing Then
    Label2.Text = firstWord 
Else
    Label2.Text = "Hash Could Not Be Cracked"
End If

另一种方法是一个简单的循环:

Dim lines = IO.File.ReadAllLines(OpenFileDialog1.FileName)
Dim matchingLine As String = Nothing
For i = 0 To lines.Length -1
    Dim line = lines(i)
    If String.Equals(MD5(line), hash.Text)) Then 
        matchingLine = line
        Exit For
    End If
Next
If matchingLine IsNot Nothing Then
    Label2.Text = matchingLine 
Else
    Label2.Text = "Hash Could Not Be Cracked"
End If
于 2012-04-26T18:47:03.740 回答
0

要停止循环,请使用Exit For

Dim words As IEnumerable(Of String) = File.ReadLines(OpenFileDialog1.FileName)

For Each word As String In words
    If MD5(word) = hash.Text Then
        Label2.Text = word
        Exit For
    Else : Label2.Text = "Hash Could Not Be Cracked"
    End If
Next

但请注意,Else这里没有意义(在离开方法之前表单不会更新)并且String.Equals可以在这里替换为=(我已经这样做了)。

于 2012-04-26T18:55:08.207 回答