将强制字符放入c#中的文本框中

时间:2010-04-05 22:23:12

标签: c# algorithm textbox passwords brute-force

我想创建一个“测试密码”的程序,看看它们需要多长时间才能打破基本暴力攻击。所以我做的是制作2个文本框。 (textbox1textbox2)并编写程序,如果文本框有输入,会出现“正确的密码”标签,但我想编写程序,以便textbox2将在其中运行强力算法,当遇到正确的密码时,它将停止。我真的需要帮助,如果你可以在我的附加代码中添加正确的添加剂就可以了。到目前为止,该程序非常简单,但我对此非常陌生,所以。

private void textBox2_TextChanged(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{


    if (textBox2.Text == textBox1.Text)
    {
        label1.Text = "Password Correct";
    }
    else
    {
        label1.Text = "Password Wrong";

    }

}


private void label1_Click(object sender, EventArgs e)
{

}

2 个答案:

答案 0 :(得分:5)

使用这个简单的强力类来“破解”你的密码。我已将此处的最大尺寸设置为 3 ,因此我无需等待太长时间。如果你有一整天都增加这个!

private class BrutePasswordGuesser
{
    private const int MaxAscii = 126;
    private const int MaxSize = 3;
    private const int MinAscii = 33;

    private int _currentLength;

    public BrutePasswordGuesser()
    {
        //Init the length, and current guess array.
        _currentLength = 0;
        CurrentGuess = new char[MaxSize];
        CurrentGuess[0] = (char) MinAscii;
    }

    public char[] CurrentGuess { get; private set; }

    public bool NextGuess()
    {
        if (_currentLength >= MaxSize)
        {
            return false;
        }

        //Increment the previous digit (Uses recursion!)
        IncrementDigit(_currentLength);

        return true;
    }

    /// <summary>
    /// Increment the character at the index by one. If the character is at the maximum 
    /// ASCII value, set it back to the minimum, and increment the previous character.
    /// Use recursion to do this, so that the proggy will step all the way back as needed.
    /// If the very bottom of the string is reached, add another character to the guess.
    /// </summary>
    /// <param name="digitIndex"></param>
    private void IncrementDigit(int digitIndex)
    {
        //Don't fall out the bottom of the array.
        //If we're at the bottom of the array, add another character
        if (digitIndex < 0)
        {
            AddCharacter();
        }
        else
        {
            //If the current character is max ASCII, set to min ASCII, and increment the previous char.
            if (CurrentGuess[digitIndex] == (char) MaxAscii)
            {
                CurrentGuess[digitIndex] = (char) MinAscii;
                IncrementDigit(digitIndex - 1);
            }
            else
            {
                CurrentGuess[digitIndex]++;
            }
        }
    }

    private void AddCharacter()
    {
        _currentLength++;
        //If we've reached our maximum guess size, leave now and don't come back.
        if (_currentLength >= MaxSize)
        {
            return;
        }
        //Initialis as min ASCII.
        CurrentGuess[_currentLength] = (char) (MinAscii);
    }
}

在上面的示例中,使用如下所示的类:

private void button1_Click(object sender, EventArgs e)
{
    var guesser = new BrutePasswordGuesser();

    var guess = new String(guesser.CurrentGuess);
    while (textBox1.Text != guess)
    {
        textBox2.Text = guess;
        if (!guesser.NextGuess())
        {
            label1.Text = "Maximum guess size reached.";
            break;
        }
        guess = new String(guesser.CurrentGuess);
    }

    if (textBox1.Text == textBox2.Text)
    {
        Label1.Text = "Password Correct";
    }
}

答案 1 :(得分:0)

需要更多信息;你是在随机猜测密码吗?字典攻击?你是否按顺序猜测密码?密码中使用的长度/字符集有哪些其他限制?

我将假设您的程序在UI中自动调用这些尝试,而不是您作为用户。如果是这种情况,我会放弃UI策略并使用控制台实现。

“随机”猜测问题很重要的原因是,如果您按顺序猜测,它所花费的时间长度与您选择的密码直接相关。我不确定你在寻找什么结果。