C#密码生成器,生成多个密码

时间:2014-01-17 22:48:19

标签: c# .net winforms

解决

我正在使用密码生成器,它可以使用一个密码。但是如果我愿意,我想生成不止一个。

我的设计看起来像这样: enter image description here

如果我选择长度超过1,它会生成一个字符的密码,但如果我想生成多个密码,我不知道该怎么做。 有什么建议?我的代码在按钮上看起来像这样:

        StringWriter sw = new StringWriter();
        length = (int)numericUpDownPasswordlength.Value;
        totalPasswords = (int)numericUpDownPassword.Value;
        string[] minatecken = symb;

        for (; length > 0; length--)
        {
            sw.Write(minatecken[rnd.Next(0, 62)]);
        }

        richTextBoxPasswords.Text = sw + "\n";

Codeanswer,感谢大卫:

        StringWriter sw = new StringWriter();
        StringBuilder sb = sw.GetStringBuilder();

        length = (int)numericUpDownPasswordlength.Value;
        totalPasswords = (int)numericUpDownPassword.Value;
        string[] minatecken = symb;

        for (; totalPasswords > 0; totalPasswords--)
        {
            for (var i = length; i > 0; i--)
            {
                sw.Write(minatecken[rnd.Next(0, 62)]);
            }

            richTextBoxPasswords.Text += sw + "\n";
            sb.Remove(0, sb.Length);
        }

2 个答案:

答案 0 :(得分:3)

好吧,你正在寻找一个值来生成一个密码,所以你只需要将它包装在另一个循环中以生成一个密码列表。该循环的每次迭代都将是您现在拥有的单个密码实现。像这样:

for (; totalPasswords > 0; totalPasswords--)
{
    for (var i = length; i > 0; i--)
    {
        sw.Write(minatecken[rnd.Next(0, 62)]);
    }
    richTextBoxPasswords.Text += sw + "\n";
}

答案 1 :(得分:1)

for循环中执行密码生成块:

  StringWriter sw = new StringWriter();
    length = (int)numericUpDownPasswordlength.Value;
    totalPasswords = (int)numericUpDownPassword.Value;

    for (int pwdCount=0; pwdCount < totalPasswords; pwdCount++)
    {
        string[] minatecken = symb;

        for (; length > 0; length--)
        {
            sw.Write(minatecken[rnd.Next(0, 62)]);
        }

        richTextBoxPasswords.Text = sw + "\n";
    }

这应该创建并显示totalPasswords

中包含的密码数量