Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- class Program
- {
- private const string CapitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- private const string SmallLeters = "abcdefghijklmnopqrstuvwxyz";
- private const string Digits = "0123456789";
- private const string SpecialChars = "~!@#$%^&*()_+='{}[]\\|':;.,/?<>";
- private const string AllChars = CapitalLetters + SmallLeters + Digits + SpecialChars;
- private static Random rnd = new Random();
- static void Main()
- {
- StringBuilder password = new StringBuilder();
- for (int i = 1; i <= 2; i++)
- {
- char capitalLetter = GenerateChar(CapitalLetters);
- InsertAtRandomPosition(password, capitalLetter);
- char smallLetter = GenerateChar(SmallLeters);
- InsertAtRandomPosition(password, smallLetter);
- }
- char digit = GenerateChar(Digits);
- InsertAtRandomPosition(password, digit);
- for (int i = 1; i <= 3; i++)
- {
- char specialChar = GenerateChar(SpecialChars);
- InsertAtRandomPosition(password, specialChar);
- }
- int count = rnd.Next(8);
- for (int i = 0; i <= count; i++)
- {
- char specialChar = GenerateChar(AllChars);
- InsertAtRandomPosition(password, specialChar);
- }
- Console.WriteLine(password);
- Console.WriteLine();
- }
- private static void InsertAtRandomPosition(StringBuilder password, char character)
- {
- int randomPosition = rnd.Next(password.Length + 1);
- password.Insert(randomPosition, character);
- }
- private static char GenerateChar(string availableChars)
- {
- int randomIndex = rnd.Next(availableChars.Length);
- char randomChar = availableChars[randomIndex];
- return randomChar;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement