2

Hi all i would like to know what would be the best way to have a password generated with at lest one number and one special character and at lest one upper case letter.

 private static string PassGEN()
    {
        int i;
        int seed = rand.Next(1,2);
        var  CaptialChars = "QWERTYUIOPASDFGHJKLZXCVBNM";
        var  smallChars = "qwertyuiopasdfghjklzxcvbnm";
        var  numbers = "9462537810";
        var  specialCharacters = @"!$%*-/=?\_";

        var sma = CaptialChars[rand.Next(1, CaptialChars.Length)];
        var sm = smallChars[rand.Next(1, smallChars.Length)];
        var smb = numbers[rand.Next(1, numbers.Length)];
        var smc = specialCharacters[rand.Next(1, specialCharacters.Length)];

        //int seeded = sma + sm + smb + smc;



        var all = CaptialChars + smallChars + numbers + specialCharacters;

        /*
        int CapLenght = 2;
        int smallLenght = 2;
        int num = 2;
        int special = 2;
        int  passwordLength = CapLenght + smallLenght + num + special;
        */

        int passwordLength = 8;
        var chars = new char[passwordLength];
        var rd = new Random(seed);

        for (i = 0; i < passwordLength; i++) 
        {
            chars[i] = all[rd.Next(0, all.Length)];
        }
         return new string(chars);
    }
David
  • 239
  • 2
  • 3
  • 12
  • 1
    The *dumb* approach is to try to generate something (completely randomly) and then check if result satisfy criteria. If it's not - repeat. – Sinatr Jun 15 '16 at 11:42
  • Using a password manager or similar library, or the framework's own classes. For example, WHY are you excluding `"` , `'` and `@` ? Do you have SQL injection issues perhaps? – Panagiotis Kanavos Jun 15 '16 at 11:43
  • `"1A!" + something` – Thomas Weller Jun 15 '16 at 11:44
  • @Sinatr I love it :) – Myrtle Jun 15 '16 at 11:45
  • Hi Sinatr, this is a Console Application that will send out passwords when accounts are due to expire, this will then make a call to the Application that will talk to the Database and pass it the password that will be generated. – David Jun 15 '16 at 11:55

0 Answers0