0

I need to generate all upper and lower case permutations of the word 'password',however sometimes I swop out the chars a for @, s for 5 and o for 0,so I need to come up with all combinations so that my dictionary can eventually crack the password.This is a homework question btw.

I have already gotten a permutation to work on upper and lowercase but im unsure as to how to proceed to the steps where I substitute letters for symbols/numbers.

public List<string> permute(String input)
        {
            var list = new List<string>();



            int n = input.Length;

            // Number of permutations is 2^n 
            int max = 1 << n;

            // Converting string 
            // to lower case 
            input = input.ToLower();

            // Using all subsequences  
            // and permuting them 
            for (int i = 0; i < max; i++)
            {
                char[] combination = input.ToCharArray();

                // If j-th bit is set, we  
                // convert it to upper case 
                for (int j = 0; j < n; j++)
                {
                    if (((i >> j) & 1) == 1)
                        combination[j] = (char)(combination[j] - 32);
                }
                string tmp = new string(combination);

                bool add = false;
                //if combination contains 32(space) ,16(0),21(5) and  dont add
                foreach (char c in combination)
                {
                    if (((c) == 32) || (c) == 16 || (c) == 21) //add 0 and 5
                    {
                        //dont add
                        add = false;
                        //break on first instance
                        break;
                    }
                    else
                    {
                        add = true;
                    }
                }
                // Printing current combination 
                Console.Write(combination);
                Console.Write(" ");
                if (add) list.Add(tmp);                
            }

            return list;
        }

static void Main(string[] args)
        {
            var pass1 = new List<string>();
            var pass2 = new List<string>();
            var pass3 = new List<string>();
            var pass4 = new List<string>();
            var pass5 = new List<string>();
            Permute p = new Permute();
            pass1 = p.permute("password");//this works well
            //Replae a with @            
            pass2 = p.permute("p@ssword"); //remove all values with no a
            //REplace o with 0
            pass3 = p.permute("passw0rd"); //remove all values with no o            
            //Replace 5 with s
            pass4 = p.permute("pa55word");
}

So to me, it seems im on the right track, but I can see that im actually going to be missing some of these sets and then im not going to crack the password.I need to use the results of the previous set somehow.

In the above im never going to get a value of say P@55w0rd, and that could be the correct answer.

Batista
  • 141
  • 1
  • 2
  • 13
  • 2
    Maybe instead of a bit (two-state value) for upper/lower you should use a three(-or-more)-state value for upper/lower/special? Then you'd get all permutations on the first call – Rafalon Aug 13 '19 at 13:11
  • Do you mind showing me what you mean? – Batista Aug 13 '19 at 13:15
  • You have 3 states: lower, upper and special case, therefore you have 3^n combinations (at most, because many characters only have 2 states). Therefore you could iterate from 0 to 3^n (instead of 2^n) and get the digit in base-3 ([this might help](https://stackoverflow.com/questions/923771/quickest-way-to-convert-a-base-10-number-to-any-base-in-net)) and depending on if the digit is 0, 1 or 2 you try to cast to lower, upper or special. If special doesn't exist, you simply discard the iteration and go to the next one – Rafalon Aug 13 '19 at 13:29
  • Thanks, im still pretty lost though. – Batista Aug 13 '19 at 14:09

1 Answers1

1

Ok so after some scraping around and head scratching - I came up with this, and it works!Well I was able to brute force the target site and received my success message!

public static List<string> Combinations(string input)
        {
            var combinations = new List<string>();

            combinations.Add(input);
            int n = input.Length;

            // Number of permutations is 2^n 
            int max = 1 << n;

            for (int i = 0; i < input.Length; i++)
            {
                char[] buffer = input.ToArray();
                if (buffer[i] == 'o')
                {
                    buffer[i] = '0';
                    combinations.Add(new string(buffer));
                    combinations = combinations.Concat(Combinations(new string(buffer))).ToList();
                }

                if (buffer[i] == 'a')
                {
                    buffer[i] = '@';
                    combinations.Add(new string(buffer));
                    combinations = combinations.Concat(Combinations(new string(buffer))).ToList();
                }

                if (buffer[i] == 's')
                {
                    buffer[i] = '5';
                    combinations.Add(new string(buffer));
                    combinations = combinations.Concat(Combinations(new string(buffer))).ToList();
                }              
            }

            for (int i = 0; i < max; i++)
            {
                char[] combination = input.ToCharArray();

                // If j-th bit is set, we  
                // convert it to upper case 
                for (int j = 0; j < n; j++)
                {
                    if (((i >> j) & 1) == 1)
                        combination[j] = (char)(combination[j] - 32);

                    //if (((i >> j) & 1) == 2)
                    //    combination[j] = (char)(combination[j] - 32);
                }
                string tmp = new string(combination);

                bool add = true;

                foreach (char c in combination)
                {
                    if (((c) == 32) || (c) == 16 || (c) == 21) //add 0 and 5
                    {
                        //dont add
                        add = false;
                        //break on first instance
                        break;
                    }
                    else
                    {
                        add = true;
                    }
                }

                if (add) combinations.Add(tmp);
            }
            return combinations;
        }
Batista
  • 141
  • 1
  • 2
  • 13