Advertisement
ivan_yosifov

Password_Generator

Nov 14th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class Program
  5. {
  6.     private const string CapitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  7.     private const string SmallLeters = "abcdefghijklmnopqrstuvwxyz";
  8.     private const string Digits = "0123456789";
  9.     private const string SpecialChars = "~!@#$%^&*()_+='{}[]\\|':;.,/?<>";
  10.     private const string AllChars = CapitalLetters + SmallLeters + Digits + SpecialChars;
  11.     private static Random rnd = new Random();
  12.     static void Main()
  13.     {
  14.         StringBuilder password = new StringBuilder();
  15.  
  16.         for (int i = 1; i <= 2; i++)
  17.         {
  18.             char capitalLetter = GenerateChar(CapitalLetters);
  19.             InsertAtRandomPosition(password, capitalLetter);
  20.             char smallLetter = GenerateChar(SmallLeters);
  21.             InsertAtRandomPosition(password, smallLetter);
  22.         }
  23.  
  24.         char digit = GenerateChar(Digits);
  25.         InsertAtRandomPosition(password, digit);
  26.  
  27.         for (int i = 1; i <= 3; i++)
  28.         {
  29.             char specialChar = GenerateChar(SpecialChars);
  30.             InsertAtRandomPosition(password, specialChar);
  31.         }
  32.  
  33.         int count = rnd.Next(8);
  34.         for (int i = 0; i <= count; i++)
  35.         {
  36.             char specialChar = GenerateChar(AllChars);
  37.             InsertAtRandomPosition(password, specialChar);
  38.         }
  39.  
  40.         Console.WriteLine(password);
  41.  
  42.         Console.WriteLine();
  43.     }
  44.  
  45.     private static void InsertAtRandomPosition(StringBuilder password, char character)
  46.     {
  47.         int randomPosition = rnd.Next(password.Length + 1);
  48.         password.Insert(randomPosition, character);
  49.     }
  50.     private static char GenerateChar(string availableChars)
  51.     {
  52.         int randomIndex = rnd.Next(availableChars.Length);
  53.         char randomChar = availableChars[randomIndex];
  54.         return randomChar;
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement