Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hello guys! I have this piece of code that I am willing to be converted to a windows forms application in c#!
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.IO;
namespace Bruteforce
{
    class BruteWriter
    {
        public static string str = Console.ReadLine();
        public static int s = Convert.ToInt32(str);
        static void Main(string[] args)
        {
            var bw = new BruteWriter();
            bw.WriteBruteStrings("");
            Console.ReadLine();
        }
        char firstRangeChar = 'a';
        char lastRangeChar = 'z';
        int stringsLenght = s;

        private void WriteBruteStrings(string prefix)
        {
            Console.WriteLine(prefix);
            if (prefix.Length == stringsLenght)
            {
                return;
            }
            for (char c = firstRangeChar; c <= lastRangeChar; c++)
            {
                WriteBruteStrings(prefix + c);
            }
        }
    }
}


Any framework will do!
So my idea is that this brute force application would start generating keys when a button is clicked, and then add them to a listbox, is this possible?
Posted
Comments
[no name] 29-May-14 10:42am    
Yes it is possible, just go do it. What do you mean any "framework will do"? You have already decided the framework is Winform.
Member 10847268 29-May-14 10:52am    
frameworks example 4.0
[no name] 29-May-14 10:55am    
Okay... and? If you think someone is going to do this for you, you are mistaken.
PIEBALDconsult 29-May-14 11:26am    
Please don't use recursion for this task.
Please don't use class fields; use parameters.
Consider using a StringBuilder.
Learn how to write an enumerator by using yield .
Sergey Alexandrovich Kryukov 29-May-14 13:09pm    
The request makes no sense. The term "convert" in inapplicable. The answer may depend on what features do you want to add with Forms or any other UI.
—SA

This is a silly application, but presumably you know that. That algorithm's not useful for anything.

Console and windowed desktop applications have somewhat different use paradigms, so it isn't so much a case of 'converting' as working out how your core algorithm should be presented in the two cases. In this case a list box and a button makes sense, and instead of Console.WriteLine, your algorithm code should call a supplied callback which adds items to a list (in the UI thread).

There's a deeper problem here though: your algorithm is not guaranteed to finish in a reasonable time (depending on what you set stringLength to be) and cannot be cancelled. That's ok in console land, where you can Ctrl+C your way out of an infinite process, but a button that starts a never-terminating call will hang your application. You either need to limit the input of stringLength or consider running the indefinite process in a background thread.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-May-14 13:09pm    
Good points, a 5.
—SA
The algorithm remains the same - unlikely to help you brute force attack a password in a reasonable time period, thankfully - all you need is to output them differently: instead of Console.Writeline, you need to pass them back to the "calling process" in some way: either by passing a ListBox through to your class (bad), passing each string back to the caller via an event (better), or by assembling a collection of strings which you pass back when you complete (simple, but annoyingly slow to update the display).

To actually display each string is simple:
C#
MyListBox.Items.Add(myNewString);
will do it...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900