-2

I'm doing a brute-force project as a school assignment and I have to get a user input (password) with a maximum length of 12 characters and "hack" it. Every input with a length of 4 characters or less goes flawlessly. However, anything longer than that, and the app freezes and for some weird reason, stops the loop and also prevents any further interaction with the UI.

I thought maybe adding a label to the Form and changing its Text every time the hacking loop is run would solve the issue. But then I realized that, since everything is running through the main Form, this is not a solution.

Here's the function that hacks the user input:

private void startbtn_Click(object sender, EventArgs e)
        {
            okay = true;
            charstring = "";
            vpasswort = "            " + passwordtb.Text.ToString();
            passwort = vpasswort.Substring(vpasswort.Length - 12);

            var time = new System.Diagnostics.Stopwatch();
            time.Start();

            do
            {
                int ende = letters.Length - 1;
                for(int hmi = 0; hmi <= ende; hmi++)
                {
                    //MessageBox.Show(charstring);
                    for (int zmi = 0; zmi <= ende; zmi++)
                    {
                        //MessageBox.Show(charstring);
                        for (int mi = 0; mi <= ende; mi++)
                        {
                            //MessageBox.Show(charstring);
                            for (int hm = 0; hm <= ende; hm++)
                            {
                                //MessageBox.Show(charstring);
                                for (int zm = 0; zm <= ende; zm++)
                                {
                                    //MessageBox.Show(charstring);
                                    for (int m = 0; m <= ende; m++)
                                    {
                                        //MessageBox.Show(charstring);
                                        for (int ht = 0; ht <= ende; ht++)
                                        {
                                            //MessageBox.Show(charstring);
                                            for (int zt = 0; zt <= ende; zt++)
                                            {
                                                //MessageBox.Show(charstring);
                                                for (int t = 0; t <= ende; t++)
                                                {
                                                    //MessageBox.Show(charstring);
                                                    for (int h = 0; h <= ende; h++)
                                                    {
                                                        //MessageBox.Show(charstring);
                                                        for (int z = 0; z <= ende; z++)
                                                        {
                                                            //MessageBox.Show(charstring);
                                                            for (int E = 0; E <= ende; E++)
                                                            {
                                                                charstring = letters[hmi] + letters[zmi] + letters[mi] + letters[hm] + letters[zm] + 
                                                                               letters[m] + letters[ht] + letters[zt] + letters[t] + letters[h] + letters[z] + 
                                                                               letters[E];
                                                                Console.WriteLine(charstring);
                                                                if(charstring == passwort)
                                                                {
                                                                    okay = false;
                                                                    time.Stop();
                                                                    goto pizza;
                                                                }
                                                                //MessageBox.Show(charstring);
                                                            }//E
                                                        }//z
                                                    }//h
                                                }//t
                                            }//zt
                                        }//ht
                                    }//m
                                }//zm
                            }//hm
                        }//mi
                    }//zmi
                }//hmi

            } while (okay == true);
        pizza:
            MessageBox.Show("Your password was hacked!");
            result.Text = charstring;
            stopwatch.Text = time.ElapsedMilliseconds.ToString() + " ms";
        }

        //tried to change something in the form but didn't work

        private void timer1_Tick(object sender, EventArgs e)
        {
            result.Text = charstring;
        }

And so here's the main question. Is there a way for me to, say, pause the loop for one or two milliseconds to prevent the Application from freezing and/or crashing? I'm pretty sure there is a way and that it's really simple and that I just have this problem because I'm new to C# and Programming in general.

EDIT: I think I have to figure out a way to make the whole hacking process go on in the background. But, again, no idea how xD

Thank you in advance for any help!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
CubeMage
  • 61
  • 9
  • 2
    You need to look into asynchronous programming, C# has several ways to do this, but they're all a little too much to write out here. – DavidG Apr 13 '19 at 11:54
  • 1
    The answer below gives you a start but it doesn't explain what any of that mean, or, more importantly, any of the pits you can fall into. Seriously, you need to Google this and find the resources yourself. – DavidG Apr 13 '19 at 11:59
  • 4
    I'd rather not, my comment isn't a full and complete answer, it's just telling you to go and find it yourself. Personally, I think you might want to remove this question as it doesn't really help anyone. – DavidG Apr 13 '19 at 12:06

2 Answers2

1

You can either do this:

private void button1_Click(object sender, EventArgs e)
{
    var worker = new BackgroundWorker();
    worker.DoWork += (_, __) =>
    {
        // do your hack here...
    };
    worker.RunWorkerCompleted += (_, __) =>
    {
        MessageBox.Show("Finito!");
    };
    worker.RunWorkerAsync();
}

or

private async void button1_Click(object sender, EventArgs e)
{
    await Task.Run(() =>
    {
         // do your hack here...
    });
    MessageBox.Show("Finito!");
}
Frank Nielsen
  • 1,546
  • 1
  • 10
  • 17
0

All these can be done using more controlled codding. I am not sure what framework you are using.

see Dispatchtimer, control the interval and then use the .stop() method if the password correct event is called or if the range is complete and passed unit is incorrect(alphanumeric instead of numbers)

This is better for gateways and all.

Please be careful of where this tool is used. obey T&Cs, law and ethics

Rikudou En Sof
  • 526
  • 1
  • 4
  • 21