生成每个字符组合,直到某个字长

Wil*_*ill 5 c# random brute-force

我将在几周后为我的计算机和信息安全课程做一个安全演示,在这个演讲中,我将展示不同攻击(字典,彩虹和暴力)的利弊.我做的字典和彩虹攻击很好,但我需要在飞行中产生暴力攻击.我需要找到一种算法,让我循环通过字母,符号和数字的每个组合,直到某个字符长度.

举个例子,对于12的字符长度,第一代和最后几代将是:

a
ab
abc
abcd
...
...
zzzzzzzzzzzx
zzzzzzzzzzzy
zzzzzzzzzzzz
Run Code Online (Sandbox Code Playgroud)

但它也会使用数字和符号,所以我很难解释......但我认为你明白了.只使用ASCII表中的符号就可以了.

我可以用一个ASCII函数用计数器做这个图片,但我无法解决这个问题.如果任何人都可以提供一些源代码(我可能会使用C#)甚至一些伪代码,我可以编写一个函数,这将是伟大的.

先感谢您.:)

dth*_*rpe 9

递归函数将允许您运行ValidChars的所有组合:

    int maxlength = 12;
    string ValidChars;
    private void Dive(string prefix, int level)
    {
        level += 1;
        foreach (char c in ValidChars)
        {
            Console.WriteLine(prefix + c);
            if (level < maxlength)
            {
                Dive(prefix + c, level);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

将有效字符集分配给ValidChars,即想要maxlength的最大字符串长度,然后调用Dive("", 0);并离开.


Tho*_*que 5

You need to generate all combinations of characters from a set of valid characters ; let's call this set validChars. Basically, each set of combinations of length N is a cartesian product of validChars with itself, N times. That's pretty easy to do using Linq:

char[] validChars = ...;

var combinationsOfLength1 =
    from c1 in validChars
    select new[] { c1 };

var combinationsOfLength2 =
    from c1 in validChars
    from c2 in validChars
    select new[] { c1, c2 };

...

var combinationsOfLength12 =
    from c1 in validChars
    from c2 in validChars
    ...
    from c12 in validChars
    select new[] { c1, c2 ... c12 };

var allCombinations =
    combinationsOfLength1
    .Concat(combinationsOfLength2)
    ...
    .Concat(combinationsOfLength12);
Run Code Online (Sandbox Code Playgroud)

Obviously, you don't want to manually write the code for each length, especially if you don't know in advance the maximum length...

Eric Lippert has an article about generating the cartesian product of an arbitrary number of sequences. Using the CartesianProduct extension method provided by the article, you can generate all combinations of length N as follows:

var combinationsOfLengthN = Enumerable.Repeat(validChars, N).CartesianProduct();
Run Code Online (Sandbox Code Playgroud)

Since you want all combinations from length 1 to MAX, you can do something like that:

var allCombinations = 
    Enumerable
        .Range(1, MAX)
        .SelectMany(N => Enumerable.Repeat(validChars, N).CartesianProduct());
Run Code Online (Sandbox Code Playgroud)

allCombinations is an IEnumerable<IEnumerable<char>>, if you want to get the results as a sequence of strings, you just need to add a projection:

var allCombinations = 
    Enumerable
        .Range(1, MAX)
        .SelectMany(N => Enumerable.Repeat(validChars, N).CartesianProduct())
        .Select(combination => new string(combination.ToArray()));
Run Code Online (Sandbox Code Playgroud)

Note that it's certainly not the most efficient solution, but at least it's short and readable...