Strong Password Generator in C#

This code snippets will let you auto-generate strong passwords. Look for the password rules in the inline comments in code

001 using System;
002 using System.Collections.Generic;
003 using System.Linq;
004 using System.Security.Cryptography.X509Certificates;
005 using System.Text;
006 using System.Threading.Tasks;
007
008 namespace PwdGen
009 {
010     class Program
011     {
012         static void Main(string[] args)
013         {
014             Console.WriteLine(GeneratePassword(815));
015             Console.ReadKey();
016         }
017
018         static string GeneratePassword(int MinLengthint MaxLength)
019         {
020             string ValidChars "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:;<>|=.,_-~!?&%@#$£€°^*§()+[] ";
021             string SpecialChars "!@#$%^&*()";
022             
023             string pwd string.Empty;
024
025             Random rnd = new Random(Guid.NewGuid().GetHashCode());
026             
027             bool NumExist false;
028             bool IsUpper false;
029             bool IsLower false;
030             bool IsSplChr false;
031             bool NoRepeat true;
032             bool NoSeq true;
033
034             while ((!NumExist || !IsUpper || !IsLower || !IsSplChr) && (NoRepeat || NoSeq) )
035             {
036                 NumExist false;
037                 IsUpper false;
038                 IsLower false;
039                 IsSplChr false;
040                 NoRepeat true;
041                 NoSeq true;
042                 
043                 pwd string.Join(string.Empty, Enumerable.Repeat(ValidCharsrnd.Next(MinLengthMaxLength 1)).Select(=> s[rnd.Next(s.Length)]).ToArray());
044                 for (int i 0pwd.Lengthi++)
045                 {
046                     // Contains at least 1 lower case letter and 1 upper case letter (all UTF-8), at least 1 number
047
048                     if (!NumExist)
049                     {
050                         NumExist = (char.IsDigit(pwd[i]) && true);
051                     }
052                     if (!IsUpper)
053                     {
054                         IsUpper = (char.IsUpper(pwd[i]) && true);
055                     }
056                     if (!IsLower)
057                     {
058                         IsLower = (char.IsLower(pwd[i]) && true);
059                     }
060
061                     // A predefined set of special chars must be present
062                     if (!IsSplChr)
063                     {
064                         IsSplChr = (SpecialChars.IndexOf(pwd[i]) >= 0);
065                     }
066
067
068                     // Not more than 2 identical characters in a row (e.g., 111 not allowed)
069                     if (pwd.Length && NoRepeat)
070                     {
071                         NoRepeat = !((pwd[i] == pwd[1]) && (pwd[i] == pwd[2]));
072                     }
073
074                     // Not any sequence of the English alphabet / numbers (above 3 letters)
075                     if (pwd.Length && NoSeq)
076                     {
077                         NoSeq = !((pwd[2] - pwd[1]) == (pwd[1] - pwd[i]));
078                     }
079                     Console.WriteLine(!NumExist +" "+ !IsUpper " " + !IsLower " " + !IsSplChr " " +  NoRepeat +NoSeq);
080                 }
081                 
082             }
083             
084             return pwd;
085
086         }
087     }
088 }
089

View in GitHub | Made with wp-showgithubfile plugin

Leave a Reply