将具有多任务的复杂代码应用于强制逻辑

时间:2016-05-10 18:19:55

标签: c# security passwords multitasking brute-force

我最近做了一个简单的蛮力。一切都按照我的预期正常进行,但是当我试图让所有事情都做多任务时都没有。

这是我目前的代码:

private static void startBruteForce(int keyLength)
    {
    var keyChars = createCharArray(keyLength, charactersToTest[0]);

    var indexOfLastChar = keyLength - 1;
    createNewKey(0, keyChars, keyLength, indexOfLastChar);
}

private static char[] createCharArray(int length, char defaultChar)
{
    return (from c in new char[length] select defaultChar).ToArray();
}

private static void createNewKey(int currentCharPosition, char[] keyChars, int keyLength, int indexOfLastChar)
{
    var nextCharPosition = currentCharPosition + 1;
    for (int i = 0; i < charactersToTestLength; i++)
    {
        keyChars[currentCharPosition] = charactersToTest[i];

        if (currentCharPosition < indexOfLastChar)
        {
            createNewKey(nextCharPosition, keyChars, keyLength, indexOfLastChar);
        }
        else
        {

            computedKeys++;

            Console.WriteLine(new string(keyChars));
            if (new string(keyChars) == "hola")
            {
                if (!isMatched)
                {
                    isMatched = true;
                    result = new string(keyChars);
                }
            }
        }
        if (isMatched) return;
    }
}

我当前的代码叫做bruteforcer:

       var estimatedPasswordLength = 3;

        while (!isMatched)
        {
            estimatedPasswordLength++;
            startBruteForce(estimatedPasswordLength);
        }

这些是我用来组合密码进行测试的字符:

'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z','A','B','C','D','E',
'F','G','H','I','J','K','L','M','N','O','P','Q','R',
'S','T','U','V','W','X','Y','Z','1','2','3','4','5',
'6','7','8','9','0','!','$','#','@','-'

要获取密码hola我必须等待大约10分钟。因此,为了减少等待的时间,我尝试使用多个任务分割这样的工作,所以也许这样做

estimatedPasswordLength++;
startBruteForce(estimatedPasswordLength);

同时没有检查相同的密码......

我感谢任何建议 - 我该怎么做?

编辑:

关于@Cicero建议,这是我尝试过的:

    Parallel.For(0, 4, i => // what should 0 and 4 be?
     {
                 if (isMatched) return;
                  Task task = Task.Factory.StartNew(() => startBruteForce(estimatedPasswordLength));
                  estimatedPasswordLength++;
      });

但是长度现在不成比例,我在控制台字符串上写了:

aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaab
aaaaaaaaaaaaaaaac

虽然estimatedPasswordLength将是4.我怎样才能得到一个解决方案,使其成比例长度?

0 个答案:

没有答案