?
Solved

How to create a password based on some rules

Posted on 2014-02-13
1
Medium Priority
?
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
cristiantm earned 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 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