Solved

How to create a password based on some rules

Posted on 2014-02-13
1
321 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

839 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