I made Password Generator on C#.
It prompts the user for the length of the password, whether it contains numbers, lowercase letters, uppercase letters or symbols, and generates a strong password from randomly added numbers, letters, or symbols.
There's the code:
using System;
namespace Password_Generator
{
class Program
{
static string Randomize(string Str)
{
Random Rand = new Random();
string Randomized = "";
while (Randomized.Length < Str.Length)
{
Randomized += Str[Rand.Next(0, Str.Length)];
}
return Randomized;
}
static void Main(string[] args)
{
while (true)
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nDo Yout Want a New Password [True], [False]? ");
Console.ForegroundColor = ConsoleColor.Yellow;
string NewPasswordStr = Console.ReadLine();
bool NewPassword;
while (bool.TryParse(NewPasswordStr, out NewPassword) == false)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("The Given Output is Unconvertable Or it's Value is Unacceptable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nDo Yout Want a New Password [True], [False]? ");
Console.ForegroundColor = ConsoleColor.Yellow;
NewPasswordStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.ForegroundColor = ConsoleColor.White;
if (NewPassword == true)
{
Console.Clear();
Console.Write("Password Length: ");
Console.ForegroundColor = ConsoleColor.Yellow;
string LenghtStr = Console.ReadLine();
int Lenght;
while (int.TryParse(LenghtStr, out Lenght) == false || int.Parse(LenghtStr) <= 0)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("The Given Output is Unconvertable Or it's Value is Unacceptable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nPassword Length: ");
Console.ForegroundColor = ConsoleColor.Yellow;
LenghtStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\n\nInclude Numbers (0 - 9) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
string IncludeNumbersStr = Console.ReadLine();
bool IncludeNumbers;
while (bool.TryParse(IncludeNumbersStr, out IncludeNumbers) == false)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nThe Given Output is Unconvertable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nInclude Numbers (0 - 9) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
IncludeNumbersStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.Write("\n\nInclude Lowercase Letters (a - z) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
string IncludeLowercaseStr = Console.ReadLine();
bool IncludeLowercase;
while (bool.TryParse(IncludeLowercaseStr, out IncludeLowercase) == false)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nThe Given Output is Unconvertable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nInclude Lowercase Letters (a - z) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
IncludeLowercaseStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\n\nInclude Uppercase Letters (A - Z) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
string IncludeUppercaseStr = Console.ReadLine();
bool IncludeUppercase;
while (bool.TryParse(IncludeUppercaseStr, out IncludeUppercase) == false)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nThe Given Output is Unconvertable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nInclude Uppercase Letters (A - X) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
IncludeUppercaseStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\n\nInclude Symbols [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
string IncludeSymbolsStr = Console.ReadLine();
bool IncludeSymbols;
while (bool.TryParse(IncludeSymbolsStr, out IncludeSymbols) == false)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nThe Given Output is Unconvertable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nInclude Symbols [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
IncludeSymbolsStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.ForegroundColor = ConsoleColor.White;
string Password = "";
while (Password.Length < Lenght)
{
Random Rand = new Random();
if (IncludeNumbers == true)
{
Password += ((char)Rand.Next(48, 58)).ToString();
}
if (Password.Length == Lenght)
{
break;
}
if (IncludeLowercase == true)
{
Password += ((char)Rand.Next(97, 123)).ToString();
}
if (Password.Length == Lenght)
{
break;
}
if (IncludeUppercase == true)
{
Password += ((char)Rand.Next(97, 123)).ToString().ToUpper();
}
if (Password.Length == Lenght)
{
break;
}
if (IncludeSymbols == true)
{
Password += ((char)Rand.Next(33, 48)).ToString().ToUpper();
}
if (Password.Length == Lenght)
{
break;
}
}
Password = Randomize(Password);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(Password);
Console.ForegroundColor = ConsoleColor.White;
}
else
{
Environment.Exit(-1);
}
}
}
}
}
Are 221 line too much for this? How to impove it? Is this a "crapcode"? How do not write crapcode? ....Thanks!