散列从文本文件中逐个检索的每个字符串

时间:2017-06-02 02:28:30

标签: vb.net

我已经启动了一个新的应用程序,用于从文本文件中检索哈希字符串。 但我无法理清从文本到列表框加载的方式,并逐个散列,保存设置并清除列表框并加载我的设置中的所有内容。 好吧,我已经排序所有的代码,但我找不到散列列表框中加载的所有字符串的方法。 在这一刻它只散列最后一个字符串 我需要逐个哈希并添加到listbox 2

这是我的代码

Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Public Class Form2

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim md5 As MD5 = System.Security.Cryptography.MD5.Create()
    Dim inputBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
    Dim hash As Byte() = md5.ComputeHash(inputBytes)
    Dim sb As New StringBuilder()
    For i As Integer = 0 To hash.Length - 1
        sb.Append(hash(i).ToString("x2"))
    Next
    Dim openfile = New OpenFileDialog()
    openfile.Filter = "Text (*.txt)|*.txt"
    If (openfile.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
        Dim myfile As String = openfile.FileName
        Dim allLines As String() = File.ReadAllLines(myfile)
        For Each line As String In allLines

            ListBox1.Items.Add(line)

            TextBox2.Text = ListBox1.Items.Add(line)
            TextBox3.Text = line
            TextBox2.Text = sb.ToString
            My.Settings.md5_hashes.Add(TextBox3.Text + "<--->" + TextBox2.Text)

            My.Settings.Save()

            ListBox1.Items.Clear()

        Next
        For Each item In My.Settings.md5_hashes
            ListBox1.Items.Add(item)
        Next
    End If

    'TextBox2.Text = sb.ToString
    'ListBox1.Items.Add(TextBox1.Text + "<--->" + TextBox2.Text)


End Sub
End Class

1 个答案:

答案 0 :(得分:0)

事实上它很容易,但是我没看到jmcilhinney告诉我在纸上写完所有的代码,你可以帮我看看是否需要去。 它只是解决方案的前沿

Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Public Class Form2

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim openfile = New OpenFileDialog()
    openfile.Filter = "Text (*.txt)|*.txt"
    If (openfile.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
        Dim myfile As String = openfile.FileName
        Dim allLines As String() = File.ReadAllLines(myfile)
        For Each line As String In allLines

            ListBox1.Items.Add(line)
            Using hasher As MD5 = MD5.Create()    ' create hash object

                ' Convert to byte array and get hash
                Dim dbytes As Byte() =
                     hasher.ComputeHash(Encoding.UTF8.GetBytes(line))

                ' sb to create string from bytes
                Dim sBuilder As New StringBuilder()

                ' convert byte data to hex string
                For n As Integer = 0 To dbytes.Length - 1
                    sBuilder.Append(dbytes(n).ToString("X2"))
                Next n

                ListBox1.Items.Add(sBuilder.ToString)
            End Using
        Next

        For Each item In My.Settings.md5_hashes
            ListBox1.Items.Add(item)
        Next

    End If

    'TextBox2.Text = sb.ToString
    'ListBox1.Items.Add(TextBox1.Text + "<--->" + TextBox2.Text)


End Sub
Shared Function GetHash(theInput As String) As String

    Using hasher As MD5 = MD5.Create()    ' create hash object

        ' Convert to byte array and get hash
        Dim dbytes As Byte() =
             hasher.ComputeHash(Encoding.UTF8.GetBytes(theInput))

        ' sb to create string from bytes
        Dim sBuilder As New StringBuilder()

        ' convert byte data to hex string
        For n As Integer = 0 To dbytes.Length - 1
            sBuilder.Append(dbytes(n).ToString("X2"))
        Next n

        Return sBuilder.ToString()
    End Using

End Function
End Class