2017-06-02 21 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異常這是我的錯誤 –

相關問題