-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomPasswordGenerator.cs
36 lines (31 loc) · 1.1 KB
/
RandomPasswordGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Text;
namespace RandomPasswordGenerator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Random Password Generator");
Console.WriteLine("--------------------------");
Console.Write("Enter the length of the password: ");
int length = Convert.ToInt32(Console.ReadLine());
string password = GenerateRandomPassword(length);
Console.WriteLine("Generated Password: " + password);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
static string GenerateRandomPassword(int length)
{
const string validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder sb = new StringBuilder();
Random rand = new Random();
for (int i = 0; i < length; i++)
{
int randomIndex = rand.Next(0, validChars.Length);
sb.Append(validChars[randomIndex]);
}
return sb.ToString();
}
}
}