2017-06-02 20 views
-1

我已经开始一个新的应用程序哈希从文本文件检索字符串。 但我无法理清从文本加载到列表框并逐个散列的方式,保存设置并清除列表框并从我的设置中加载所有内容。 嗯,我已经对所有的代码进行排序,但是我找不到散列列表框中加载的所有字符串的方法。 在这一刻它只是哈希最后一个字符串 ,我需要散列逐一添加到列表框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 
+0

你的代码没有任何意义。如果你的目标是在文件中对每一行进行散列,那么当然你需要在代码中计算访问文件中每一行的循环内部的散列。你只在那里计算一个哈希值,它甚至在你打开文件之前。也许你应该尝试写下你实际需要执行的步骤,然后编写代码来实现这些步骤。如果你这样做,我非常怀疑你最终会打开文件,然后不计算任何哈希值。在写代码之前,你应该知道你的代码应该做什么,而你显然不知道。 – jmcilhinney

+0

嗯,是的,我想散列每一行从导入的文件 –

+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 
+0

我要说的一件事是,没有必要为文件的每一行创建一个新的'MD5'对象。你可以创建一个并重用它。你也可以在一行中从Byte数组创建一个十六进制的'String':'myHexString = String.Concat(myByteArray.Select(Function(b)b.ToString(“X2”)))'。 – jmcilhinney

+0

我坦克的重播和帮助 –

+0

嗯,事实上,我在Visual调试模式下运行Visual Basic 2012中的应用程序每一件事都是正确的,但然后温我试着打开应用程序与出Visual Studio它来Nulled异常这是我的错误 –

相关问题