Bruteforce in .NET

A VB.Net class on how to perform a string based permutation. Also known as "bruteforce". Good to have. It's not around on the net, so i chose to publish it.

Option Explicit On
Option Strict On
Public Class Permutation
    Private _Charset As String = vbNullString
    Private _IndexArray(_Max) As Integer
    Private _Max As Short = &H1
    Private _Min As Short = &H0
    Private _TimePerCalculation As Double = &H0
    Private _Estimater As UInt16 = &H0
    Private _Watch As New Stopwatch
    Public Sub New(ByVal Charset As String, ByVal Max As Short, _
                   ByVal Min As Short)
        _Charset = Charset
        _Max = Max
        _Min = Min
        ReDim _IndexArray(_Max)
        For Nullifier As Integer = &H0 To (_Min - &H1)
            _IndexArray(Nullifier) = &H1
        Next
        _Watch.Start()
    End Sub
    Public Sub Add()
        _IndexArray(&H0) = _IndexArray(&H0) + &H1
        For Index As Short = &H0 To CShort(_Max - &H1)
            If (_IndexArray(Index) > &H0) And _
               (_IndexArray(Index) > Len(_Charset)) Then
                _IndexArray(Index) = &H1
                _IndexArray(Index + &H1) = _IndexArray(Index + &H1) + &H1
            End If
        Next Index
    End Sub
    Public Function GenerateString(Optional ByVal AddToNextTurn As Boolean = True) As String
        _Estimater = CUShort(_Estimater + &H1)
        If _Estimater Mod UInt16.MaxValue = &H0 Then
            _TimePerCalculation = (_Watch.ElapsedMilliseconds / UInt16.MaxValue)
            _Watch.Reset()
            _Watch.Start()
            _Estimater = &H0
        End If
        Dim Result As String = vbNullString
        For Index As Short = &H0 To _Max
            If _IndexArray(Index) <> &H0 Then
                Result = Result & Mid(_Charset, _IndexArray(Index), &H1)
            End If
        Next
        If AddToNextTurn = True Then Add()
        Return Result
    End Function
    Public ReadOnly Property GetExitValue() As Double
        Get
            Dim Result As Double = &H0
            For Index As Integer = _Min To _Max
                Result += (Len(_Charset) ^ Index)
            Next
            Return Result
        End Get
    End Property
    Public ReadOnly Property GetExitString() As String
        Get
            Return New String(CChar(_Charset.Substring(_Charset.Length - &H1, &H1)), _Max)
        End Get
    End Property
    Public ReadOnly Property TimePerCalculation() As Double
        Get
            Return _TimePerCalculation
        End Get
    End Property
    Public Function MakeMD5(ByVal Word As String) As String
        Dim MD5 As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim ByteHash() As Byte
        Dim Result As String = vbNullString
        ByteHash = MD5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(Word))
        For Index As Integer = &H0 To &HF
            Result &= ByteHash(Index).ToString("x").PadLeft(&H2, CChar("0"))
        Next
        Return Result
    End Function
End Class

The New sub specifies the charset, the minimum and maximum length to use.

Every time you call the GenerateString() function, it will return a new permutated value.

Also, in order to determine when you're done or not. You can use neither the GetExitString() function, and see if it matches with the GenerateString() or if the number of permutations made by GenerateString() is equal to the GetExitValue().

Edit:
Press here to download the sourcecode.

Enough said. Now it's documented!

Hey, I'm Fredrik. I'm from Sweden, born 1990, and I got a huge interest for information technology and information security. So far, I've been studying for three years at the Internation IT College of Sweden and one year at the Royal Institute of Technology (Kista, Sweden). I'm one of the Co-Founders of Detectify. I'm working closely together with the swedish firm Young & Skilled. ...Not to forget, I'm the previous founder of Arctic Security. If you wish to contact me, please email me at h@ackack.net or follow me on twitter @Almroot.

1 Comment

  1. Tweets that mention Bruteforce in .NET -- Topsy.com says:

    [...] This post was mentioned on Twitter by AckAck. AckAck said: Bruteforce in .NET, updated ( http://h.ackack.net/?p=121 ): A VB.Net class on how to perfo... [...]

Leave a Comment