Solved

How to create a password based on some rules

Posted on 2014-02-13
1
319 Views
Last Modified: 2014-02-19
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
0
Comment
Question by:mousemat24
1 Comment
 
LVL 3

Accepted Solution

by:
cristiantm earned 500 total points
ID: 39856484
The function that you have just creates a random string from the characters on "chars" array.

You could use a similar function 3 times with 3 different arrays as input and size 1, one with letters, other with capital letters and other with numbers, and concatenate this to the result.  Then  you would get the minimal chars you need. After that you could use some random sorting to make the order of the chars also random.

Moreover, you could for each result character do and extra random round to select if it should be a upper case, lower case or number, and you could force a loop if the random draw did not mathed the minimum criteria you want.

BUT, i would try to find a library that just does what you want. Like this:
http://www.codeproject.com/Articles/423229/CsharpRandomStringGenerator
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question