Password generator in C#

Sheonarayan
Posted by in C# category on for Intermediate level | Points: 250 | Views : 3024 red flag
Rating: 4 out of 5  
 1 vote(s)

This article explains how to create online password generator in C#. After going through this we will be able to create secure password, based on the input, guid, letters and numbers password etc. We shall generate 4 digit, 8 characters and more strong random and safe passwords in C#.net.


 Download source code for Password generator in C#

Recommendation
Read Using jqLite and jQuery in AngularJS before this article.

Introduction

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
  1. length - how many characters of password to generate
  2. 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.

Summary

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.

Recommendation
Read AngularJS : Service vs factory vs provider after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Sheonarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Posted by: Rajayadav on: 11/27/2015 | Points: 25
Nice,Thanks for sharing :)
Posted by: Amatya on: 3/9/2016 | Points: 25
Fantastic 1 .. I was not knowing these all.. Thanks for Sharing Sir.

Login to post response

Comment using Facebook(Author doesn't get notification)