Solved

How to create a password based on some rules

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now