How to create a random password generator in C#

Datetime:2016-08-22 22:14:44          Topic: C#           Share

In this post I will show you how to create your own password generator in C#. See this code in action on my toolPassword Generator

It can be used to generate random strong passwords and it complies with recommendations from OWASP , including:

Requirements

  • Password must meet at least 3 out of the following 4 complexity rules
    • at least 1 uppercase character (A-Z)
    • at least 1 lowercase character (a-z)
    • at least 1 digit (0-9)
    • at least 1 special character (punctuation) — do not forget to treat space as special characters too
  • at least 10 characters (I've set it at 8 to satisfy some old password systems, but you can change this)
  • at most 128 characters
  • not more than 2 identical characters in a row (e.g., 111 not allowed)

If you want to skip to the end, you can do so by clicking

We will create single method which you pass parameters into and you get back a password.

There will be a list of available characters to randomly choose from and create the password.

We will add the options for which password rules we would like to enforce on each request, so these will be parameters in the constructor. The method will need to return the password as a string.

Now we know the above, we can start to create our method.

namespace CodeShare.Library.Passwords
{
public static class PasswordGenerator
{
/// <summary>
/// Generates a random password based on the rules passed in the parameters
/// </summary>
/// <param name="includeLowercase">Bool to say if lowercase are required</param>
/// <param name="includeUppercase">Bool to say if uppercase are required</param>
/// <param name="includeNumeric">Bool to say if numerics are required</param>
/// <param name="includeSpecial">Bool to say if special characters are required</param>
/// <param name="includeSpaces">Bool to say if spaces are required</param>
/// <param name="lengthOfPassword">Length of password required. Should be between 8 and 128</param>
/// <returns></returns>
public static string GeneratePassword(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSpecial, bool includeSpaces, int lengthOfPassword)
{
return "not random";
}
}
}

The next stage is to create a character set based on the parameters in the constructor.

namespace CodeShare.Library.Passwords
{
public static class PasswordGenerator
{
/// <summary>
/// Generates a random password based on the rules passed in the parameters
/// </summary>
/// <param name="includeLowercase">Bool to say if lowercase are required</param>
/// <param name="includeUppercase">Bool to say if uppercase are required</param>
/// <param name="includeNumeric">Bool to say if numerics are required</param>
/// <param name="includeSpecial">Bool to say if special characters are required</param>
/// <param name="includeSpaces">Bool to say if spaces are required</param>
/// <param name="lengthOfPassword">Length of password required. Should be between 8 and 128</param>
/// <returns></returns>
public static string GeneratePassword(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSpecial, bool includeSpaces, int lengthOfPassword)
{
const int MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS = 2;
const string LOWERCASE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
const string UPPERCASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string NUMERIC_CHARACTERS = "0123456789";
const string SPECIAL_CHARACTERS = @"!#$%&*@\";
const string SPACE_CHARACTER = " ";
const int PASSWORD_LENGTH_MIN = 8;
const int PASSWORD_LENGTH_MAX = 128;
if (lengthOfPassword < PASSWORD_LENGTH_MIN || lengthOfPassword > PASSWORD_LENGTH_MAX)
{
return "Password length must be between 8 and 128.";
}
string characterSet = "";

if (includeLowercase)
{
characterSet += LOWERCASE_CHARACTERS;
}
if (includeUppercase)
{
characterSet += UPPERCASE_CHARACTERS;
}
if (includeNumeric)
{
characterSet += NUMERIC_CHARACTERS;
}
if (includeSpecial)
{
characterSet += SPECIAL_CHARACTERS;
}
if (includeSpaces)
{
characterSet += SPACE_CHARACTER;
}
return "not random";
}
}
}

This will give us a string variable called characterSet which is made up of all of the selected character types based on the parameters.

Now we need to write the part which randomly selects characters from this set and creates the password.

namespace CodeShare.Library.Passwords
{
public static class PasswordGenerator
{
/// <summary>
/// Generates a random password based on the rules passed in the parameters
/// </summary>
/// <param name="includeLowercase">Bool to say if lowercase are required</param>
/// <param name="includeUppercase">Bool to say if uppercase are required</param>
/// <param name="includeNumeric">Bool to say if numerics are required</param>
/// <param name="includeSpecial">Bool to say if special characters are required</param>
/// <param name="includeSpaces">Bool to say if spaces are required</param>
/// <param name="lengthOfPassword">Length of password required. Should be between 8 and 128</param>
/// <returns></returns>
public static string GeneratePassword(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSpecial, bool includeSpaces, int lengthOfPassword)
{
const int MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS = 2;
const string LOWERCASE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
const string UPPERCASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string NUMERIC_CHARACTERS = "0123456789";
const string SPECIAL_CHARACTERS = @"!#$%&*@\";
const string SPACE_CHARACTER = " ";
const int PASSWORD_LENGTH_MIN = 8;
const int PASSWORD_LENGTH_MAX = 128;
if (lengthOfPassword < PASSWORD_LENGTH_MIN || lengthOfPassword > PASSWORD_LENGTH_MAX)
{
return "Password length must be between 8 and 128.";
}
string characterSet = "";

if (includeLowercase)
{
characterSet += LOWERCASE_CHARACTERS;
}
if (includeUppercase)
{
characterSet += UPPERCASE_CHARACTERS;
}
if (includeNumeric)
{
characterSet += NUMERIC_CHARACTERS;
}
if (includeSpecial)
{
characterSet += SPECIAL_CHARACTERS;
}
if (includeSpaces)
{
characterSet += SPACE_CHARACTER;
}
char[] password = new char[lengthOfPassword];
int characterSetLength = characterSet.Length;
System.Random random = new System.Random();
for (int characterPosition = 0; characterPosition < lengthOfPassword; characterPosition++)
{
password[characterPosition] = characterSet[random.Next(characterSetLength - 1)];
}
return string.Join(null, password);
}
}
}

The random variable is an object which gives you a random number each time you ask for a new one. I've passed in the length of the character set so it selects a random position in the character set.

I loop through the character positions of the new password and set them with random characters from the character set.

One of the rules from OWASP in their guidelines is that you shouldn't have more than two identical characters next to each other. So the next improvement is to add a check for that and pick a different character if it is the case on the third one.

namespace CodeShare.Library.Passwords
{
public static class PasswordGenerator
{
/// <summary>
/// Generates a random password based on the rules passed in the parameters
/// </summary>
/// <param name="includeLowercase">Bool to say if lowercase are required</param>
/// <param name="includeUppercase">Bool to say if uppercase are required</param>
/// <param name="includeNumeric">Bool to say if numerics are required</param>
/// <param name="includeSpecial">Bool to say if special characters are required</param>
/// <param name="includeSpaces">Bool to say if spaces are required</param>
/// <param name="lengthOfPassword">Length of password required. Should be between 8 and 128</param>
/// <returns></returns>
public static string GeneratePassword(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSpecial, bool includeSpaces, int lengthOfPassword)
{
const int MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS = 2;
const string LOWERCASE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
const string UPPERCASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string NUMERIC_CHARACTERS = "0123456789";
const string SPECIAL_CHARACTERS = @"!#$%&*@\";
const string SPACE_CHARACTER = " ";
const int PASSWORD_LENGTH_MIN = 8;
const int PASSWORD_LENGTH_MAX = 128;
if (lengthOfPassword < PASSWORD_LENGTH_MIN || lengthOfPassword > PASSWORD_LENGTH_MAX)
{
return "Password length must be between 8 and 128.";
}
string characterSet = "";

if (includeLowercase)
{
characterSet += LOWERCASE_CHARACTERS;
}
if (includeUppercase)
{
characterSet += UPPERCASE_CHARACTERS;
}
if (includeNumeric)
{
characterSet += NUMERIC_CHARACTERS;
}
if (includeSpecial)
{
characterSet += SPECIAL_CHARACTERS;
}
if (includeSpaces)
{
characterSet += SPACE_CHARACTER;
}
char[] password = new char[lengthOfPassword];
int characterSetLength = characterSet.Length;
System.Random random = new System.Random();
for (int characterPosition = 0; characterPosition < lengthOfPassword; characterPosition++)
{
password[characterPosition] = characterSet[random.Next(characterSetLength - 1)];
bool moreThanTwoIdenticalInARow =
characterPosition > MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS
&& password[characterPosition] == password[characterPosition - 1]
&& password[characterPosition - 1] == password[characterPosition - 2];
if (moreThanTwoIdenticalInARow)
{
characterPosition--;
}
}
return string.Join(null, password);
}
}
}

This is how you would use the code.

using System;
public class Program
{
public static void Main()
{
Console.WriteLine(CodeShare.Library.Passwords.PasswordGenerator.GeneratePassword(true, true, true, false, false, 16));
}
}

Finally we need to test that it contains at least one of the selected character types. We do that by using regular expressions in a separate method called PasswordIsValid

I can write some code to keep generating passwords until it produces one which is valid.

Final Code

//Written by Paul Seal. Licensed under MIT. Free for private and commercial uses.

using System;
using System.Text.RegularExpressions;
using CodeShare.Library.Passwords;

public class Program
{
public static void Main()
{
bool includeLowercase = true;
bool includeUppercase = true;
bool includeNumeric = true;
bool includeSpecial = false;
bool includeSpaces = false;
int lengthOfPassword = 16;

string password = PasswordGenerator.GeneratePassword(includeLowercase, includeUppercase, includeNumeric, includeSpecial, includeSpaces, lengthOfPassword);

while(!PasswordGenerator.PasswordIsValid(includeLowercase, includeUppercase, includeNumeric, includeSpecial, includeSpaces, password))
{
password = PasswordGenerator.GeneratePassword(includeLowercase, includeUppercase, includeNumeric, includeSpecial, includeSpaces, lengthOfPassword);
}

Console.WriteLine(password);
}
}
namespace CodeShare.Library.Passwords
{
public static class PasswordGenerator
{
/// <summary>
/// Generates a random password based on the rules passed in the parameters
/// </summary>
/// <param name="includeLowercase">Bool to say if lowercase are required</param>
/// <param name="includeUppercase">Bool to say if uppercase are required</param>
/// <param name="includeNumeric">Bool to say if numerics are required</param>
/// <param name="includeSpecial">Bool to say if special characters are required</param>
/// <param name="includeSpaces">Bool to say if spaces are required</param>
/// <param name="lengthOfPassword">Length of password required. Should be between 8 and 128</param>
/// <returns></returns>
public static string GeneratePassword(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSpecial, bool includeSpaces, int lengthOfPassword)
{
const int MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS = 2;
const string LOWERCASE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
const string UPPERCASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string NUMERIC_CHARACTERS = "0123456789";
const string SPECIAL_CHARACTERS = @"!#$%&*@\";
const string SPACE_CHARACTER = " ";
const int PASSWORD_LENGTH_MIN = 8;
const int PASSWORD_LENGTH_MAX = 128;
if (lengthOfPassword < PASSWORD_LENGTH_MIN || lengthOfPassword > PASSWORD_LENGTH_MAX)
{
return "Password length must be between 8 and 128.";
}
string characterSet = "";

if (includeLowercase)
{
characterSet += LOWERCASE_CHARACTERS;
}
if (includeUppercase)
{
characterSet += UPPERCASE_CHARACTERS;
}
if (includeNumeric)
{
characterSet += NUMERIC_CHARACTERS;
}
if (includeSpecial)
{
characterSet += SPECIAL_CHARACTERS;
}
if (includeSpaces)
{
characterSet += SPACE_CHARACTER;
}
char[] password = new char[lengthOfPassword];
int characterSetLength = characterSet.Length;
System.Random random = new System.Random();
for (int characterPosition = 0; characterPosition < lengthOfPassword; characterPosition++)
{
password[characterPosition] = characterSet[random.Next(characterSetLength - 1)];
bool moreThanTwoIdenticalInARow =
characterPosition > MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS
&& password[characterPosition] == password[characterPosition - 1]
&& password[characterPosition - 1] == password[characterPosition - 2];
if (moreThanTwoIdenticalInARow)
{
characterPosition--;
}
}
return string.Join(null, password);
}

/// <summary>
/// Checks if the password created is valid
/// </summary>
/// <param name="includeLowercase">Bool to say if lowercase are required</param>
/// <param name="includeUppercase">Bool to say if uppercase are required</param>
/// <param name="includeNumeric">Bool to say if numerics are required</param>
/// <param name="includeSpecial">Bool to say if special characters are required</param>
/// <param name="includeSpaces">Bool to say if spaces are required</param>
/// <param name="password">Generated password</param>
/// <returns>True or False to say if the password is valid or not</returns>
public static bool PasswordIsValid(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSpecial, bool includeSpaces, string password)
{
const string REGEX_LOWERCASE = @"[a-z]";
const string REGEX_UPPERCASE = @"[A-Z]";
const string REGEX_NUMERIC = @"[\d]";
const string REGEX_SPECIAL = @"([!#$%&*@\\])+";
const string REGEX_SPACE = @"([ ])+";
bool lowerCaseIsValid = !includeLowercase || (includeLowercase && Regex.IsMatch(password, REGEX_LOWERCASE));
bool upperCaseIsValid = !includeUppercase || (includeUppercase && Regex.IsMatch(password, REGEX_UPPERCASE));
bool numericIsValid = !includeNumeric || (includeNumeric && Regex.IsMatch(password, REGEX_NUMERIC));
bool symbolsAreValid = !includeSpecial || (includeSpecial && Regex.IsMatch(password, REGEX_SPECIAL));
bool spacesAreValid = !includeSpaces || (includeSpaces && Regex.IsMatch(password, REGEX_SPACE));
return lowerCaseIsValid && upperCaseIsValid && numericIsValid && symbolsAreValid && spacesAreValid;
}
}
}

Here is the final code in a dotnetfiddle. Try changing the bool variables and password length in the calling method:

If you would like to see this code being used in a web based password generator tool, you see the tool I made using this code.Password Generator

I hope you enjoyed this post and I hope it helps you with generating passwords or learning how to write C#.

Please feel free to share or leave a comment.





About List