473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

generate randaom password(1 uuper case letter, 1 digit and 6 character length)

Hi,

I want generate random password. it should contain at least 1 upper case letter, 1 digit, 6 character length.

I can use Membership.GeneratePassword(-, -), but its not having numbers some times.

Any help please?

Thank you.
Jul 16 '10 #1
1 5970
Hi Ramanak,

Here's a quick algorithm to do want you want to do. Sorry it's not the cleanest but it works. Didn't have plenty of time to test it though.

You can change the minimum and maximum number of chars. You can also add other chars (symbols, etc) if you want. You can also make it cleaner, but don't create the Random object in the function or it will always generate the same password.

Again, sorry it's not the cleanest but I think you get the idea.

Have a good day.

Tomy


Expand|Select|Wrap|Line Numbers
  1.         protected String RandomPassword()
  2.         {
  3.             String pwd = "";
  4.             int length = 0;
  5.             int index = 0;
  6.             int numericIndex = -1;
  7.             int upperCaseIndex = -1;
  8.  
  9.             //Length of your password
  10.             length = rnd.Next(MINLENGTH, MAXLENGTH);
  11.  
  12.             // You generate a password of the desired length
  13.             for (int i = 0; i < length; i++)
  14.             {
  15.                 // Generate an index that smaller than the size of your allowed chars
  16.                 index = rnd.Next(0, allowedChars.Length);
  17.  
  18.                 pwd += allowedChars[index];
  19.             }
  20.  
  21.             ////*********************************************************
  22.             // We make sure that there is at least one numeric
  23.             // Replace one random char by a numeric
  24.             numericIndex = rnd.Next(0, pwd.Length);
  25.  
  26.             // Generate a numeric, delete one char and replace it with a numeric
  27.             index = rnd.Next(0, numericChars.Length);
  28.             pwd = pwd.Remove(numericIndex, 1);
  29.             pwd = pwd.Insert(numericIndex, numericChars[index].ToString());
  30.             ////*********************************************************
  31.  
  32.  
  33.             ////*********************************************************
  34.             // We make sure that there is at least one uppercase
  35.             // Replace one random char by a numeric
  36.             upperCaseIndex = rnd.Next(0, pwd.Length);
  37.  
  38.             // We make sure our uppercase index is different
  39.             // from our numeric index or we will overwrite our
  40.             // only numeric value with our uppercase value
  41.             while (upperCaseIndex == numericIndex)
  42.             {
  43.                 upperCaseIndex = rnd.Next(0, pwd.Length);
  44.             }
  45.  
  46.             // Generate a numeric, delete one char and replace it with a numeric
  47.             index = rnd.Next(0, upperCaseChars.Length);
  48.             pwd = pwd.Remove(upperCaseIndex, 1);
  49.             pwd = pwd.Insert(upperCaseIndex, upperCaseChars[index].ToString());
  50.             ////*********************************************************
  51.  
  52.             return pwd;
  53.         }
  54.  
  55.         Random rnd = new Random();
  56.         const int MINLENGTH = 6;
  57.         const int MAXLENGTH = 20;
  58.         const String allowedChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  59.         const String upperCaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  60.         const String numericChars = "0123456789";
  61.  
Jul 16 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Adams-Blake Co. | last post by:
I want to allow the user to enter her own username and password. I want to validate the password the way lots of programs do.... that it has to be: - 6 or more characters. - must be at...
4
by: avillela | last post by:
I'm looking for a password generator. example: I pass as parameter a string and the function return a password.(string) can anyone provide me any password generator ???? thanks André -...
3
by: learn.2005 | last post by:
Hello Friends! I am Manu Gupta, an IT Engineering student, learning PHP presently. I want to match user name and pwd from MySQL, it shows case insensitiveness. Can anybody help me in implementing...
1
by: bigd7788 | last post by:
Ladies and gents, Need a little help with the following: Write a program that will accept a string from the user and evaluate it as a password, giving it a passing or a failing grade. A good...
4
by: Chris | last post by:
I want to ensure a password has a least one lower case letter, one upper letter and a number. I'm a bit of a newbie but I understand you can use the pipe system for OR what about AND.I would like...
3
by: Army1987 | last post by:
Is there anything wrong with this program? It seems to behave strangely if I give stdin EOF when asked for the character set... /* BEGIN pwdgen.c */ #include <stdio.h> #include "random.h"...
2
by: DarthPeePee | last post by:
Hello everyone. I am working on a Password Strength Meter and I am running into 1 problem that I would like to fix. When pressing the "Clear Password & Try Again" button, the password clears...
5
by: ashurack | last post by:
I found a stored procedure online a while back and want to inplement it. The only problem is that it doesn't check to see if the number generated is currently in use in the DB. I know it's really...
1
by: ccarter45 | last post by:
I'm new to java and writing a program that accepts user input for a password and it has to meet the following requirements: 1. At least 6 characters long. 2. Leading character can't be a digit....
0
by: Jonathan Boivin | last post by:
If I could give an advice on your method, in fact, what I would do.. is : Generate a password with your lower security set of characters. Then upper case randomizally some letters and finally...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.