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.