There are many scenarios where we need password generator program. Sometimes we need password generator of 2 words, 3 digit, 4 digit or sometimes password generator 8 characters, password generator 16 characters or 32 characters or 64 characters, strong password generator or random password generator. Here we will see password generator application that uses password generator code in C#.
Password generator in C# using Membership provider - Complex password
There is a dedicated method in System.Web.Security.Membership
class to generate password that accepts two parameters
- length - how many characters of password to generate
- numberOfNonAlphaNumericCharacters - how many non alphanumeric characters to keep in the generated password.
protected void btnMembership_Click(object sender, EventArgs e)
{
lblPassword.Text = System.Web.Security.Membership.GeneratePassword(8, 3);
}
On click of the button, above method executes that set the generated password to the Label. Here, this code will generate 8 characters long password that will have 3 non alphanumeric characters.
This method generates very complex password and we have complete control over how much more complex password we want to generate by controlling both parameters.
Password generator based on input in C#
Here we will see that how to generate password of specific length based on our input. Below method has a string variable named valid
that contains the characters from which we want to generate our password.
To increase or decrease the length of password to generate, set the lengthOfPassword
variable value.
protected void btnCustomChar_Click(object sender, EventArgs e)
{
int lengthOfPassword = 8;
string valid = "abcdefghijklmnozABCDEFGHIJKLMNOZ1234567890";
StringBuilder strB = new StringBuilder(100);
Random random = new Random();
while (0 < lengthOfPassword--)
{
strB.Append(valid[random.Next(valid.Length)]);
}
lblPassword.Text = strB.ToString();
}
After that we are creating a Random object and getting the random character from the valid
string and appending to the StringBuilder. At last, setting the generated password into Label.
In this case also we have complete control over how complex password we want to generate. To generate more complex password in this case, simply change the valid string value.
// to generate more complex password, change valid string value
string valid = "abcdefghijklmnozABCDEFGHIJKLMNOZ1234567890!@#$%^&*()-=";
Password generator using Crypto service provider in C#
A password generator program can also be developed using Crypto service provider. There are many crypto service provider available in System.Security.Cryptography namespace. We can use any one of them and follow the same approach that we have followed for SHA512CryptoServiceProvider.
protected void btnUsingCrypto_Click(object sender, EventArgs e)
{
int lengthOfPassword = 8;
string passCode = DateTime.Now.Ticks.ToString();
string pass = BitConverter.ToString(new System.Security.Cryptography.SHA512CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(passCode))).Replace("-", String.Empty);
lblPassword.Text = pass.Substring(0, lengthOfPassword);
}
Here, we have computed the hash code from the current date time ticks and replaced "-" with empty string so that we do not get "-" character in string. After that retrieved number of characters we want from the generated string.
To increase the length of password characters, simply change the value of lengthOfPassword
variable.
Password generator code using C# GUID
We can also get a random password string using GUID in C#. Here we are calling the NewGuid() method of Guid object and replacing "-" with empty and then retrieving the number of characters we want to get from the generated Guid string.
protected void btnGuid_Click(object sender, EventArgs e)
{
int lengthOfPassword = 8;
string guid = Guid.NewGuid().ToString().Replace("-", "");
lblPassword.Text = guid.Substring(0, lengthOfPassword);
}
Simple letters and numbers password generator in C#
We can also use GetRandomFileName() method of the System.IO.Path class that generates random file name with random extension name.
protected void btnSimpleFile_Click(object sender, EventArgs e)
{
lblPassword.Text = System.IO.Path.GetRandomFileName().Replace(".", "");
}
After getting the random file name, we have replaced the extension name separator "." with empty string that gives us simple password that contains only alphanumeric characters.
In this article, we saw how to generate complex and rather simple password in C# code. The Membership method generates very complex password, the second method of chosen character gives us complete control of what characters we want in the password. Remaining three methods gives random password that contains alphanumeric characters.