I created a random password generator with in a windows form.
The user will select the length of the password and the number of passwords which will be generated in a Richtextbox.
The problem arises when i want to generate more than 1 password.
It does not generate more than 1 password at a time no matter how i do it.
Posting code below.
string passwords = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890?!/@£#$%&[]+``^~*";
Random pass = new Random();
string passwordline { get; set; }
private void button1_Click(object sender, EventArgs e)
{
passwordline = "";
int passwordsNum = (int)numericUpDown1.Value;
int passwordLength = (int)numericUpDown2.Value;
int length = 0;
for (int i = 0; i<passwordsNum; i++)
{
for (int i2 = 0; i2 < passwordLength; i2++)
{
int randomnumbers = pass.Next(passwords.Length);
passwordline += passwords[randomnumbers];
length++;
if (passwordline.Length == passwordLength)
{
richTextBox1.Text += passwordline + Environment.NewLine;
}
}
}
}
It seems that it could be that you are not resetting the password to empty with every new run. Try changing code to this:
for (int i = 0; i<passwordsNum; i++)
{
passwordline = "";
for (int i2 = 0; i2 < passwordLength; i2++)
{
//Other code is the same
}
}
You simply need to add a new line between two passwords, now you generate sequences of characters you "concatenate" into one password (and remove some errors):
string passwordline = "";
int passwordsNum = (int)numericUpDown1.Value;
int passwordLength = (int)numericUpDown2.Value;
for (int i = 0; i<passwordsNum; i++) {
passwordline = "";
for (int i2 = 0; i2 < passwordLength; i2++) {
int randomnumbers = pass.Next(passwords.Length);
passwordline += passwords[randomnumbers];
}
richTextBox1.Text += passwordline+"\n";
}
The code is also more optimized: you use an if
statement in your for
-loop, while this code is never executed and simply should be executed at the end of your statement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.