Link to home
Start Free TrialLog in
Avatar of mousemat24
mousemat24

asked on

How to create a password based on some rules

Hi there

I have this code

 public static string GetUniqueKey(int maxSize)
        {
            char[] chars = new char[62];
            chars =
            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
            byte[] data = new byte[1];
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            data = new byte[maxSize];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(maxSize);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return result.ToString();
        }

Open in new window


var newPassword = GetUniqueKey(8);  

Open in new window


I found an error with this, in that, I want to create a unique password, with the following:

AT LEAST

1 Uppercase
1 Lowercase
1 Number
... and MUST have at least 8 char

the problem with the code above is, it could create a password like KKoaoiuw

i.e. no number, or no lowercase etc

Does anyone know how to change it so that I have

AT LEAST

1 Uppercase
1 Lowercase
1 Number
... and MUST have at least 8 char


I found this reg exp

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}$

Open in new window


Dont know how to add that in, matter of fact dont know if it would help?
thanks
ASKER CERTIFIED SOLUTION
Avatar of cristiantm
cristiantm
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial