473,698 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
Hi,

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

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

Any help please?

Thank you.
Jul 16 '10 #1
1 6042
Tomy Rodrigue
3 New Member
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
4819
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 least one upper and one lower case letter in the password. - must be at least one number in the password.
4
1635
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é - Brazil
3
1575
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 case sensitiveness in either PHP or MySQL? Thanks in advance
1
1413
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 password will be at least 8 characters and include at least one lower-case letter, at least one upper-case letter, at least one digit and at least one character that is neither a letter nor a digit. Your program will need to check each character in the...
4
4731
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 something like this. &&
3
5303
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" #include <stdlib.h> #include <string.h> #define MAX_PWD_LEN 128
2
3199
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 out of the text box, but the meter will stay at its current position until text is entered back into the textbox. Once text is re-entered, the meter will display the results again. I would like everything to reset when the button is pushed, but I...
5
5990
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 unlikely for the same number to be generated twice (with a 10 digit number) but it's for a shopping cart so I need it to be 100% reliable. I thought about opening a cursor then looping through each distinct record in the DB but couldn't figure...
1
1655
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. 3. Password must have at least 1 digit. 4. Password must have at least 1 upper case letter. The main method will keep asking the user for a password (using a while loop) until it gets one that's okay. The other method has the signature: public...
0
1250
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 inserts special chars at randomized positions. I don't know if you like the proposition, but it can help. Jonathan Boivin "Morten Snedker" <morten.snedker@gmail.coma écrit dans le message de news:
0
8672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4361
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2323
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.