在Visual Basic中暴力破解MD5哈希

时间:2014-06-26 08:36:09

标签: vb.net cryptography md5

我已经制作了一个程序,使用MD5算法将字符串转换为哈希。

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

    bytesToHash = md5Obj.ComputeHash(bytesToHash)

    Dim strResult As String = ""
    Dim b As Byte

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

    Return strResult
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    TextBox2.Text = getMD5Hash(TextBox1.Text)
End Sub

但是现在我添加了一个按钮,在给出MD5哈希时会使用强力来查找原始文本。

我可以在按钮中编写哪些代码来系统地尝试每个单词/数字并根据原始哈希值进行检查?

3 个答案:

答案 0 :(得分:0)

  

我可以在按钮中编写什么代码来系统地尝试每一个   单词/数字,并根据原始哈希检查它?

看看当前的代码以及你创建Button的巨大努力,我建议,这可能听起来有点疯狂,但我真正推荐的按钮中的代码.... VB.NET

答案 1 :(得分:0)

您永远无法找到有保证的原始文字。最多可以找到一段具有相同哈希值的文本,因此可能或可能不是原始代码。

你的第二个问题是MD5是一个加密哈希,因此明确设计用来阻止某人做你想做的事情。实际上,您正在MD5上尝试Preimage attack

这不是不可能的,但这很困难,你的最终节目可能需要几十年时间才能找到匹配的原像。

我建议您解决相同问题的简单版本,而不是花费那么多时间。从一个简短的非加密哈希开始,比如说32位FNV hash。代码哈希,然后尝试查找一些产生与原始文本相同的哈希的文本。 FNV有不同的尺寸,所以你可以尝试FNV-32,FNV-64,FNV-128等。这将让你看到增加散列大小对寻找Preimage难度的影响。

最重要的是,确保你玩得开心。

答案 2 :(得分:0)

我完成了程序,代码非常基础。它可能有一些错误。

    Dim done As Boolean

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

    bytesToHash = md5Obj.ComputeHash(bytesToHash)

    Dim strResult As String = ""
    Dim b As Byte

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

    Return strResult
    End Function 
'introduces MD5 Function


 Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As        System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

     'Added a background worker because I needed a stop button. 

    Dim password As String
    password = TextBox4.Text
    'the password is going to be in form of a hash, this is the hash the program will try to brute force  

    If TextBox4.Text = "" Then
        MsgBox("Please Enter a Password or Hash")

    Else

        Dim test() As String = {"", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
                                "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
                                "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "~", "!", "@", "#", "$", "%", "^",
                                "&", "*", "(", ")", "_", "-", "+", "="}
       'This array has all the characters the program will include in it's permutations and combinations



        For x As Integer = 0 To test.Length - 1
            Me.Button2.Enabled = True
            Console.WriteLine(test(x))
            For y As Integer = 0 To test.Length - 1
                Console.WriteLine(test(x) & test(y))
                For z As Integer = 0 To test.Length - 1
                    Console.WriteLine(test(x) & test(y) & test(z))
                    For q As Integer = 0 To test.Length - 1
                        Console.WriteLine(test(x) & test(y) & test(z) & test(q))
                        For j As Integer = 0 To test.Length - 1
                            Console.WriteLine(test(x) & test(y) & test(z) & test(q) & test(j))
  'this code runs the loop. It tries the characters in the "test" array. 
  'The maximum number of combination it can try is 5. (practically too slow to reach that number)

   TextBox3.Text = test(x) & test(y) & test(z) & test(q) & test(j)
                            TextBox3.Refresh()
                            Me.Refresh()
 'Textbox displays the combinations 


   If getMD5Hash(test(x) & test(y) & test(z) & test(q) & test(j)) = password Then
   'The word generated in the loop is converted in it's MD5 hash form. 

   MsgBox("Password is:" & test(x) & test(y) & test(z) & test(q) & test(j))
 If the hashed password matches the generated hash then a message is displayed:    Password is [the password in word form]

                              done = True
               'boolean done is set to true             
                     TextBox4.Text = ""
                     TextBox3.Text = ""
   End If



                        Next
                        If done = True Then Exit Sub
                    Next
                    If done = True Then Exit Sub
                Next
                If done = True Then Exit Sub
            Next
            If done = True Then Exit Sub
        Next
    End If
'This section stops the loop if the password is found OR if boolean done is true

    If done = True Then
        done = False
    End If
    TextBox3.Enabled = True
    TextBox4.Enabled = True 'after loop is completed the text boxes are enabled

End Sub


Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    done = True
    TextBox3.Enabled = True
    TextBox4.Enabled = True
'This button sets done to true and this stops the loop
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    done = False
    If done = False Then
        BackgroundWorker1.RunWorkerAsync()
    End If
    TextBox3.Enabled = False
    TextBox4.Enabled = False
'This button starts the loop by running backgroundWorker and disables the textboxes
End Sub


End Class