Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Article

Simple Password Generator

Rate me:
Please Sign up or sign in to vote.
2.53/5 (5 votes)
8 Jun 2008CPOL2 min read 34.3K   467   10   4
Dynamic generation of password

Introduction

Generating password is not a big deal. Pool the character from the character array and you will be generating password easily. This example shows how easily you can dynamically generate a password.Background

Using the code

PWDGenerator.cs uses System.Collections namespace to apply ArrayList. You can set the length of the password by setting the passwordlength to the length as you need.

This example simply demonstrate the basic use of Arraylist and Random number to generate random combinations or alphabets and numbers.

public static string GeneratePWD()

{

int passwordLength = 10;

int quantity = 1;

ArrayList arrCharPool = new ArrayList();

Random rndNum = new Random();

arrCharPool.Clear();

string password = "";

//Lower Case

for (int i = 97; i < 123; i++)

{

arrCharPool.Add(Convert.ToChar(i).ToString());

}

//Number

for (int i = 48; i < 58; i++)

{

arrCharPool.Add(Convert.ToChar(i).ToString());

}

//Upper Case

for (int i = 65; i < 91; i++)

{

arrCharPool.Add(Convert.ToChar(i).ToString());

}

for (int x = 0; x < quantity; x++)

{

//Iterate through the number of characters required in the password

for (int i = 0; i < passwordLength; i++)

{

password += arrCharPool[rndNum.Next(arrCharPool.Count)].ToString();

}

}

return password;

}

Build the application and start clicking the generate button. Instantly you will notice the changes. Here what I have done is selecting the integer values and converting them in the character which I have added to ArrayList and finally iterated through the generated characters from the ArrayList and generated a string of the specified length.

This example is simple and definitely you are going to enjoy by using it in your application.

History

  • Initial release - 9th June 2008.

Conclusion

From this article we can simply generate an algorithm to generate random strings in the format as we have designed. Suppose you want to generate a password in more redable form for the new users signed up in your application or website and you need to send them password, then in that case you can simply use the ramdom strings generated from the above application as a password to the new users.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer SAPNEP IT SOLUTION
Nepal Nepal
Working as a .Net programmer for SapNep IT Solution.

URL: http://www.sapnep.com/

Comments and Discussions

 
QuestionWhat? Pin
PIEBALDconsult8-Jun-08 18:52
mvePIEBALDconsult8-Jun-08 18:52 
AnswerRe: What? Pin
Vasudevan Deepak Kumar8-Jun-08 19:58
Vasudevan Deepak Kumar8-Jun-08 19:58 
PIEBALDconsult wrote:
It's also not worthy of an article.


Little rude.

The user seems to be a novice. Also, he seems to be from a little downtrodden region. Though it does not qualify as an article at the first sight, let us try putting a few words of encouragement and see if the author is motivated and pull him up the ladder.

Vasudevan Deepak Kumar
Personal Homepage
Tech Gossips


All the world's a stage,
And all the men and women merely players.
They have their exits and their entrances;
And one man in his time plays many parts... --William Shakespeare




GeneralRe: What? Pin
RajuMaharjan9-Jun-08 22:16
RajuMaharjan9-Jun-08 22:16 
GeneralTo make it more comprehensive... Pin
Vasudevan Deepak Kumar8-Jun-08 18:44
Vasudevan Deepak Kumar8-Jun-08 18:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.