Better way to do a Bruteforce on strings?

113 views Asked by At

I have come up with a pretty efficient method of bruteforcing a string in C#. (Yes, it could be used as a password cracker/generator)

Basically it works like this (this example is using a 5 character length string with only lowercase letters):

  1. Create an array that is 5 indices in length.
  2. Start a loop that will increment the array similarly to the way a numeral system increments it's place value (1 0 0 0 0, or 1 1 0 0 0). Yes, I do it backwards. I work from Left to Right, not like our decimal system which works from Right to Left.
  3. Transform those numbers(or place values) into actual characters. This is done by using those numbers to point to an index in an array that holds all the target characters.

The code for this example is below.

using System;
using System.Text;

public class Program
{
    public static void Main(string[] args)
    {       
        NumeralSystem sys = new NumeralSystem(9, new Range(0, Helpers.Chars.Length - 1));
        sys.PrintStrings();

        Console.ReadKey(true);
    }
}

public class NumeralSystem
{
    private int _Length;
    private Range _Range;

    public int Length
    {
        get
        {
            return _Length;
        }
    }

    public Range Range
    {
        get
        {
            return _Range;
        }
    }

    public NumeralSystem(int length, Range range)
    {
        _Length = length;
        _Range = range;
    }

    public NumeralSystem(int length, int min, int max)
    {
        _Length = length;
        _Range = new Range(min, max);
    }


    public void PrintStrings()
    {
        int num = this.Range.Max;
        int[] arr = new int[this.Length];
        for (int c = 0; c < arr.Length; c++) arr[c] = this.Range.Min;
        while (true)
        {
            arr[0]++;
            if (arr[0] > this.Range.Max)
            {
                for (int i = 1; i < arr.Length; i++)
                {
                    if (arr[i - 1] > this.Range.Max)
                    {
                        arr[i - 1] = this.Range.Min;
                        arr[i]++;
                    }
                }
            }

            int num2 = arr[0];
            for (int i = 1; i < arr.Length; i++) num2 += arr[i];

            //PrintArray(arr);
            Console.WriteLine(Helpers.ArrayToString(arr));

            if (num2 == this.Range.Max * arr.Length) break;
        }
    }

    private void PrintArray(int[] arr)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            if (i == arr.Length - 1)
            {
                Console.WriteLine(arr[i]);
            }
            else
            {
                Console.Write(arr[i] + ", ");
            }
        }
    }
}

public class Helpers
{
    public static char[] Chars = new char[] { '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' };
    public static string ArrayToString(int[] arr)
    {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] >= 0)
            {
                builder.Append(Helpers.Chars[arr[i]]);
            }
            else
            {
                throw new Exception();
            }
        }
        return builder.ToString();
    }
}

public class Range
{
    private int _Max, _Min;

    public int Max
    {
        get
        {
            return _Max;
        }
    }

    public int Min
    {
        get
        {
            return _Min;
        }
    }

    public Range(int min, int max)
    {
        _Max = max;
        _Min = min;
    }
}

So is there any faster method to bruteforce a string than this? When I say "brute force a string", I mean find all possible combinations of the string, given the length, and possible characters. Thanks for your help in advance.

0

There are 0 answers