Created
November 20, 2013 15:51
-
-
Save Ex094/7565457 to your computer and use it in GitHub Desktop.
A small C# Snippet to generate random password of a given length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Coded By Ex094 | |
Random rnd = new Random(); //To Generate Random Numbers for string Index | |
string password = ""; //Variable to store the final Password | |
string passlist = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,./'[]~!@#$%^&*()_+=-|}{:?><"; | |
Console.WriteLine ("Enter Password Length:"); | |
string usrleng = Console.ReadLine(); //Get length from user in the form of string | |
int length = Convert.ToInt32(usrleng); //Convert the string to an int | |
for (int x = 0; x < length; x++) //Loop accroding to the length of the password | |
{ | |
int pick = rnd.Next(1, passlist.Length); //Generate index number (Random) | |
password += passlist[pick]; //Get the random item from the passlist based on the random index number | |
} | |
Console.WriteLine("Your Generated Password Is: {0}", password); //Print the password |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment