I started learning C# a few days ago and thought that a random password generator would be a good starter project to do. It worked with letters only but now that I also want to include arrays of numbers and symbols it often outputs the wrong amount of characters like in this example:
Heres my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace randompassword
{
internal class Program
{
static void Main(string[] args)
{
// Variables
int pwlength;
string includeSym;
string includeNum;
// Dictionary
char[] symbols = { '?', '!', '$', '€', '%' };
char[] letters = { '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' };
int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.Write("Choose your password length: ");
pwlength = Convert.ToInt32(Console.ReadLine());
Console.Write("Include numbers (Y/N): ");
includeNum = Console.ReadLine();
if (includeNum != "Y" && includeNum != "N")
{
Console.WriteLine("Invalid answer - Only answer the letter Y for Yes and N for No! ");
Console.Write("Include numbers (Y/N): ");
includeNum = Console.ReadLine();
}
Console.Write("Include symbols? (Y/N): ");
includeSym = Console.ReadLine();
if (includeSym != "Y" && includeSym != "N")
{
Console.WriteLine("Invalid answer - Only answer the letter Y for Yes and N for No! ");
Console.Write("Include symbols? (Y/N): ");
includeSym = Console.ReadLine();
}
Random rnd = new Random();
for (int z = 1; z <= pwlength; z++)
{
int rndList = rnd.Next(1, 4);
if (rndList == 1)
{
int rndChar = rnd.Next(0, 53);
Console.Write(letters[rndChar]);
}
else if (rndList == 2)
{
int rndNum = rnd.Next(0, 11);
Console.Write(nums[rndNum]);
}
else if (rndList == 3)
{
int rndSym = rnd.Next(0, 6);
Console.Write(symbols[rndSym]);
}
}
Console.ReadKey();
}
}
}
I would suggest something like this instead:
var characters = new List<char>(){ 'a', 'b', ...
Console.Write("Include numbers (Y/N): ");
var includeNum = Console.ReadLine();
if(if (includeNum.ToLower() == "y"){
characters.AddRange( new []{ '?', '!', '$', '€', '%' });
}
// Do the same for numbers
Console.Write("Choose your password length: ");
var pwlength = Convert.ToInt32(Console.ReadLine());
var password = Enumerable.Range(0, pwLength).Select(i => characters[rnd.Next(0, characters.Count)).ToString();
Console.WriteLine(password);
This should reduce the risk of any index out of bound exceptions, as well as giving a more even probability of characters.
Note that this should only be used for learning. Any proper password generator should use a secure random generator, as well ass omitting similar letters, like I/l and O/0.