Friday, April 08, 2005

Generating Random Passwords

Here are few ways of generating random passwords:

// First technique is to use the GUID:
string guID = System.Guid.NewGuid().ToString();


// This will generate 21 new unique passwords

string alphabets = "abcdefghijklmnopqrstuvwxyz";
string numbers = "01234567890123456789012345";
string password = null;
Random r = new Random();
for(int j=0; j<=20;j++) { for(int i=0;i<=5;i++) { password += alphabets[r.Next(alphabets.Length)]; password += numbers[r.Next(numbers.Length)]; } Response.Write(password); password = null; Response.Write("
");
}
return password;


Since strings are immutable which means each time a new copy of the string will be made for each concatenation operation so for performance you should use StringBuilder class to concatenate the strings.

No comments: