【问题标题】:I need to write a program that generates all possible combinations of the word password in upper and lowercase我需要编写一个程序,生成大小写单词密码的所有可能组合
【发布时间】:2019-08-13 13:03:15
【问题描述】:

我需要生成单词“密码”的所有大写和小写排列,但是有时我将字符 a 替换为 @,s 替换为 5,o 替换为 0,所以我需要想出所有组合以便我的字典最终可以破解密码。顺便说一句,这是一道作业题。

我已经对大小写进行了排列,但我不确定如何继续执行将字母替换为符号/数字的步骤。

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");
}

所以对我来说,似乎我在正确的轨道上,但我可以看到我实际上会丢失其中一些集合,然后我不会破解密码。我需要使用前一组的结果不知何故。

在上面我永远不会得到 P@55w0rd 的值,这可能是正确的答案。

【问题讨论】:

  • 也许你应该对上/下/特殊使用三(或更多)状态值而不是位(双态值)?然后你会在第一次调用时得到所有排列
  • 你介意告诉我你的意思吗?
  • 您有 3 种状态:小写、大写和特殊情况,因此您有 3^n 种组合(最多,因为许多字符只有 2 种状态)。因此,您可以从 0 迭代到 3^n(而不是 2^n)并获取以 3 为底的数字(this might help),并根据数字是 0、1 还是 2,您尝试将其转换为较低、较高或特别。如果不存在特殊的,您只需丢弃迭代并转到下一个
  • 谢谢,不过我还是很迷茫。

标签: c# permutation


【解决方案1】:

好吧,经过一番摸索和挠头后 - 我想出了这个,它有效!好吧,我能够暴力破解目标站点并收到我的成功消息!

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;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 2015-12-10
    • 1970-01-01
    • 2015-07-26
    • 2022-08-24
    • 2020-07-28
    • 2017-10-15
    • 1970-01-01
    相关资源
    最近更新 更多