-2

I'm a math student with little to no experience programming, but I wrote this to act like a brute force algorithm. It seems to run fine except that it runs all the password combinations out to 3 characters for passwords as short as 2. Also I'm sure there's a way to refactor the for and if statements as well. Any help would be appreciated, thanks.

I've already been testing to see if some of the if statements aren't executing, and it looks like the statements with "console.writeln(Is this executing)" aren't executing but I'm not really sure.

public Form1()
    {
        InitializeComponent();
    }
    static char[] Match ={'0','1','2','3','4','5','6','7','8','9','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','C','L','M','N','O','P',
                    'Q','R','S','T','U','V','X','Y','Z','!','?',' ','*','-','+'};
    private string[] tempPass;
    private void button1_Click(object sender, EventArgs e)
    {
        string tempPass1 = "lm";
        string result = String.Empty;
        int passLength = 1;
        int maxLength = 17;
        tempPass = new string[passLength];
        for (int i = 0; i < Match.Length; i++)
        {
            if (tempPass1 != result)
            {
                tempPass[0] = Match[i].ToString();
                result = String.Concat(tempPass);

                if (passLength > 1)
                {

                    for (int j = 0; j < Match.Length; j++)
                    {
                        if (tempPass1 != result)
                        {
                            tempPass[1] = Match[j].ToString();
                            result = String.Concat(tempPass);
                            if (passLength > 2)
                            {

                                for (int k = 0; k < Match.Length; k++)
                                {

                                    if (tempPass1 != result)
                                    {
                                        tempPass[2] = Match[k].ToString();
                                        result = String.Concat(tempPass);
                                    if (tempPass[0] == "+" && tempPass[1] == "+" && tempPass[2] == "+" && tempPass1 != result)
                                        {
                                            Console.WriteLine("This will execute?");
                                            passLength++;
                                            tempPass = new string[passLength];
                                            k = 0;
                                            j = 0;
                                            i = 0;
                                        }
                                        else if (result == tempPass1)
                                        {
                                            Console.WriteLine("Broken");
                                            Console.WriteLine("This is big gay: " + result);
                                            break;
                                        }


                                    }

                                }
                            }
                            if (tempPass[0] == "+" && tempPass[1] == "+" && tempPass1 != result)
                            {
                                Console.WriteLine("Did this execute?");
                                passLength++;
                                tempPass = new string[passLength];
                                j = 0;
                                i = 0;
                            }
                            else if (result == tempPass1)
                            {
                                Console.WriteLine("Broken");
                                Console.WriteLine("This is bigger gay: " + result);
                                break;
                            }

                        }

                    }
                }
                //tempPass[1] = "World!";
                //Console.WriteLine(result);
                if (tempPass[tempPass.Length - 1] == "+" && tempPass1 != result)
                {
                    passLength++;
                    tempPass = new string[passLength];
                    Console.WriteLine(tempPass.Length + "  " + result + "  " + "Success");
                    Console.WriteLine(i);
                    i = 0; /**update
                    j = 0;
                    k = 0;
                    l = 0;
                    m = 0;*/
                }
                else if (result == tempPass1)
                {
                    Console.WriteLine("Broken");
                    Console.WriteLine("This is biggest gay: " + result);

                }
            }
        }
    }
  • 4
    This is a great time to start using a debugger. With your debugger you can place a breakpoint anywhere you want to pause the execution of the code and from there you can step through the code line by line as it executes, observing the exact runtime behavior and the changing values of the variables on each line. When you do that, on which line does your program first produce an unexpected result? What were the values of the variables before that line executed? What was the observed result? What was the expected result? Why? – David Jan 20 '19 at 15:00
  • 1
    **[Navigating through Code using the Step Debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx)** – Ňɏssa Pøngjǣrdenlarp Jan 20 '19 at 15:07
  • I've gone through it and it produces an unexpected result in the for (int k = 0;) line. The value before of the list length was 2 and then after it is 3. I expected it to not even execute that line of code. – Christheyankee Jan 20 '19 at 15:26
  • @Christheyankee: Time to continue debugging. You expect a variable to equal 2, but it equals 3? Why do you expect it to equal 2? There are a finite number of lines which modify the variable. As you step through the debugger, observe when those lines execute. Currently all you're really saying is that you don't expect an `if` block to execute, but it does. The answer to that is that the `if` condition is then clearly `true`. We can certainly help as you drive to a more specific question, but currently you're just asking us to debug your program for you. Which isn't something we do. – David Jan 20 '19 at 15:42
  • I highly recommend the [Combinatorics Libary](https://www.codeproject.com/Articles/26050/%2FArticles%2F26050%2FPermutations-Combinations-and-Variations-using-C-G) written by Adrian Akison. – Idle_Mind Jan 20 '19 at 15:55

1 Answers1

0

Play with this; modified from my answer here. It'll show you all the 2 and 3 length combinations. Clicking the button will start/stop the generation process. You need a button, label, and a timer:

public partial class Form1 : Form
{

    private Revision rev;

    public Form1()
    {
        InitializeComponent();
        rev = new Revision("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!? *-+", "00");
        label1.Text = rev.CurrentRevision;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = !timer1.Enabled;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        rev.NextRevision();
        if (rev.CurrentRevision.Length == 4)
        {
            timer1.Stop();
            MessageBox.Show("Sequence Complete");

            // make it start back at the beginning?
            rev = new Revision("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!? *-+", "00");
            label1.Text = rev.CurrentRevision;
        }
        else
        {
            label1.Text = rev.CurrentRevision;
        }
    }
}

public class Revision
{

    private string chars;
    private char[] values;

    private System.Text.StringBuilder curRevision;

    public Revision()
    {
        this.DefaultRevision();
    }

    public Revision(string validChars)
    {
        if (validChars.Length > 0)
        {
            chars = validChars;
            values = validChars.ToCharArray();
            curRevision = new System.Text.StringBuilder(values[0]);
        }
        else
        {
            this.DefaultRevision();
        }
    }

    public Revision(string validChars, string startingRevision)
        : this(validChars)
    {
        curRevision = new System.Text.StringBuilder(startingRevision.ToUpper());
        int i = 0;
        for (i = 0; i <= curRevision.Length - 1; i++)
        {
            if (Array.IndexOf(values, curRevision[i]) == -1)
            {
                curRevision = new System.Text.StringBuilder(values[0]);
                break;
            }
        }
    }

    private void DefaultRevision()
    {
        chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        values = chars.ToCharArray();
        curRevision = new System.Text.StringBuilder(values[0]);
    }

    public string ValidChars
    {
        get { return chars; }
    }

    public string CurrentRevision
    {
        get { return curRevision.ToString(); }
    }

    public string NextRevision(int numRevisions = 1)
    {
        bool forward = (numRevisions > 0);
        numRevisions = Math.Abs(numRevisions);
        int i = 0;
        for (i = 1; i <= numRevisions; i++)
        {
            if (forward)
            {
                this.Increment();
            }
            else
            {
                this.Decrement();
            }
        }
        return this.CurrentRevision;
    }

    private void Increment()
    {
        char curChar = curRevision[curRevision.Length - 1];
        int index = Array.IndexOf(values, curChar);
        if (index < (chars.Length - 1))
        {
            index = index + 1;
            curRevision[curRevision.Length - 1] = values[index];
        }
        else
        {
            curRevision[curRevision.Length - 1] = values[0];
            int i = 0;
            int startPosition = curRevision.Length - 2;
            for (i = startPosition; i >= 0; i += -1)
            {
                curChar = curRevision[i];
                index = Array.IndexOf(values, curChar);
                if (index < (values.Length - 1))
                {
                    index = index + 1;
                    curRevision[i] = values[index];
                    return;
                }
                else
                {
                    curRevision[i] = values[0];
                }
            }
            curRevision.Insert(0, values[0]);
        }
    }

    private void Decrement()
    {
        char curChar = curRevision[curRevision.Length - 1];
        int index = Array.IndexOf(values, curChar);
        if (index > 0)
        {
            index = index - 1;
            curRevision[curRevision.Length - 1] = values[index];
        }
        else
        {
            curRevision[curRevision.Length - 1] = values[values.Length - 1];
            int i = 0;
            int startPosition = curRevision.Length - 2;
            for (i = startPosition; i >= 0; i += -1)
            {
                curChar = curRevision[i];
                index = Array.IndexOf(values, curChar);
                if (index > 0)
                {
                    index = index - 1;
                    curRevision[i] = values[index];
                    return;
                }
                else
                {
                    curRevision[i] = values[values.Length - 1];
                }
            }
            curRevision.Remove(0, 1);
            if (curRevision.Length == 0)
            {
                curRevision.Insert(0, values[0]);
            }
        }
    }

}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40