SIGN UP MEMBER LOGIN:    
ARTICLE

Password Generator

Posted by Jay Smith Articles | Visual C# June 04, 2003
This article and the attached source code shows you how to write a password generator using C#.
Reader Level:
Download Files:
 

What were going to make today is a password generator. Its real simple but can be quite simple but could come and handy some day. For example I use it in Asp.Net if people registries on my site I give them a randomly generated password so that people has to give there real e-mail address. 

Open a new project like you always add a new class to the project name the class PassGen or whatever you want the class to be called.
We only need one namespace (Yeah! life can be easy some days) System;

We need two things :

protected Random rGen;
protected string[] strCharacters = { "A","B","C","D","E","F","G",
"H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y",
"Z","1","2","3","4","5","6","7","8","9","0","a","b","c","d","e","f","g","h",
"i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

You have to put this in the Constructor

public PassGen()
{
rGen =
new Random();
}

rGen provide us a randomly generated number and strCharacters has all characters we need in a array.

Now we will make two methods one for generate password which has only lowercase numbers and characters and one that makes both lowercase and uppercase characters.

public string GenPassLowercase(int i)
{
int p = 0;
string strPass = "";
for (int x = 0; x<= i; x++)
{
p = rGen.Next(0,35);
strPass += strCharacters[p];
}
return strPass.ToLower();
}

And one for the both upper/lowercase characters

public string GenPassWithCap(int i)
{
int p = 0;
string strPass = "";
for (int x = 0; x<= i; x++)
{
p = rGen.Next(0,60);
strPass += strCharacters[p];
}
return strPass;
}

As you may notice the integer i let us choose how many characters we want in our password. Ok that was it quite easy and I think there was nothing what could cause a problem. Hope it will me from some value to you.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor