Click here to Skip to main content
11,706,477 members (31,825 online)
Click here to Skip to main content

A C# Password Generator

, 17 Aug 2003 321.9K 8.8K 128
Rate this:
Please Sign up or sign in to vote.
An article on implementing a simple password generator class in C#

Introduction

This article illustrates how to create a very simple password generator using C#. Password generators are useful in many applications:

  • Registration/membership systems for Web sites
  • Auto-creation of passwords according to a specified rule
  • Securing application-specific data

The PasswordGenerator class is fairly simple. It exposes several properties that control how the password will be generated.

  • Exclusions: Specifies the set of characters to exclude in password generation.
  • Minimum: Specifies the minimum length of the generated password.
  • Maximum: Specifies the maximum length of the generated password.
  • ConsecutiveCharacters: Controls generation of consecutive characters in the generated password.
  • RepeatingCharacters: Controls generation of repeating characters in the generated password.
  • ExcludeSymbols: Excludes symbols from the set of characters used to generate the password

After setting your desired properties, call the Generate() method to create your new password.

namespace WorkingCode.CodeProject.PwdGen
{
    using System;
    using System.Security.Cryptography;
    using System.Text;

    public class PasswordGenerator
    {
        public PasswordGenerator() 
        {
            this.Minimum               = DefaultMinimum;
            this.Maximum               = DefaultMaximum;
            this.ConsecutiveCharacters = false;
            this.RepeatCharacters      = true;
            this.ExcludeSymbols        = false;
            this.Exclusions            = null;

            rng = new RNGCryptoServiceProvider();
        }		
		
        protected int GetCryptographicRandomNumber(int lBound, int uBound)
        {   
            // Assumes lBound >= 0 && lBound < uBound
            // returns an int >= lBound and < uBound
            uint urndnum;   
            byte[] rndnum = new Byte[4];   
            if (lBound == uBound-1)  
            {
                // test for degenerate case where only lBound can be returned
                return lBound;
            }
                                                              
            uint xcludeRndBase = (uint.MaxValue -
                (uint.MaxValue%(uint)(uBound-lBound)));   
            
            do 
            {      
                rng.GetBytes(rndnum);      
                urndnum = System.BitConverter.ToUInt32(rndnum,0);      
            } while (urndnum >= xcludeRndBase);   
            
            return (int)(urndnum % (uBound-lBound)) + lBound;
        }

        protected char GetRandomCharacter()
        {            
            int upperBound = pwdCharArray.GetUpperBound(0);

            if ( true == this.ExcludeSymbols )
            {
                upperBound = PasswordGenerator.UBoundDigit;
            }

            int randomCharPosition = GetCryptographicRandomNumber(
                pwdCharArray.GetLowerBound(0), upperBound);

            char randomChar = pwdCharArray[randomCharPosition];

            return randomChar;
        }
        
        public string Generate()
        {
            // Pick random length between minimum and maximum   
            int pwdLength = GetCryptographicRandomNumber(this.Minimum,
                this.Maximum);

            StringBuilder pwdBuffer = new StringBuilder();
            pwdBuffer.Capacity = this.Maximum;

            // Generate random characters
            char lastCharacter, nextCharacter;

            // Initial dummy character flag
            lastCharacter = nextCharacter = '\n';

            for ( int i = 0; i < pwdLength; i++ )
            {
                nextCharacter = GetRandomCharacter();

                if ( false == this.ConsecutiveCharacters )
                {
                    while ( lastCharacter == nextCharacter )
                    {
                        nextCharacter = GetRandomCharacter();
                    }
                }

                if ( false == this.RepeatCharacters )
                {
                    string temp = pwdBuffer.ToString();
                    int duplicateIndex = temp.IndexOf(nextCharacter);
                    while ( -1 != duplicateIndex )
                    {
                        nextCharacter = GetRandomCharacter();
                        duplicateIndex = temp.IndexOf(nextCharacter);
                    }
                }

                if ( ( null != this.Exclusions ) )
                {
                    while ( -1 != this.Exclusions.IndexOf(nextCharacter) )
                    {
                        nextCharacter = GetRandomCharacter();
                    }
                }

                pwdBuffer.Append(nextCharacter);
                lastCharacter = nextCharacter;
            }

            if ( null != pwdBuffer )
            {
                return pwdBuffer.ToString();
            }
            else
            {
                return String.Empty;
            }	
        }
            
        public string Exclusions
        {
            get { return this.exclusionSet;  }
            set { this.exclusionSet = value; }
        }

        public int Minimum
        {
            get { return this.minSize; }
            set	
            { 
                this.minSize = value;
                if ( PasswordGenerator.DefaultMinimum > this.minSize )
                {
                    this.minSize = PasswordGenerator.DefaultMinimum;
                }
            }
        }

        public int Maximum
        {
            get { return this.maxSize; }
            set	
            { 
                this.maxSize = value;
                if ( this.minSize >= this.maxSize )
                {
                    this.maxSize = PasswordGenerator.DefaultMaximum;
                }
            }
        }

        public bool ExcludeSymbols
        {
            get { return this.hasSymbols; }
            set	{ this.hasSymbols = value;}
        }

        public bool RepeatCharacters
        {
            get { return this.hasRepeating; }
            set	{ this.hasRepeating = value;}
        }

        public bool ConsecutiveCharacters
        {
            get { return this.hasConsecutive; }
            set	{ this.hasConsecutive = value;}
        }

        private const int DefaultMinimum = 6;
        private const int DefaultMaximum = 10;
        private const int UBoundDigit    = 61;

        private RNGCryptoServiceProvider    rng;
        private int 			minSize;
        private int 			maxSize;
        private bool			hasRepeating;
        private bool			hasConsecutive;
        private bool            hasSymbols;
        private string          exclusionSet;
        private char[] pwdCharArray = "abcdefghijklmnopqrstuvwxyzABCDEFG" +
            "HIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_=+[]{}\\|;:'\",<" + 
            ".>/?".ToCharArray();                                        
    }
}

The previous version of this class was intended for use in a much larger project (I will be publishing articles on various components of that project soon.). Due to many factors, that code was rushed and proved to be inefficient. In fact, I wish I could go back in time and unpublish it! While this version is definitely better, there is still room for improvement. The generation algorithm can still be optimized. Also, it would be interesting to use regular expressions to both define and validate the passwords we wish to generate. I would have done this, but it's been a long time since I wrote a parser. Maybe for the next version...

In previous articles, I have used the NAnt tool as my build solution. Unfortunately, that team has not produced a stable release that integrates NUnit 2.0. I COULD get the source from the CVS tree, but I'm way too lazy for that. Instead, I have decided to go back to Visual Studio .NET as my development environment. I'm also getting used to test-driven development with NUnit 2.0. If you aren't using this tool for unit testing, I highly recommend you give it a try http://www.nunit.org/. Its use of attributes and reflection to specify test suites, test fixtures and tests, and is quite remarkable and easy to use. I've included my unit test fixture with the source code. Also, try the NUnit Addin for Visual Studio .NET; it's very handy for running your tests within the IDE.

The demo project is a simple Windows Forms UI that allows one to configure the password generator's properties. I must say that while VS.NET is fairly complete and powerful, I just don't like the feel of the forms designer. However, it definitely does the job.

Many thanks to Mike Asher and Julian Roberts for their feedback on the first version of the password generator. Julian was kind enough to test the code in an ASP.NET project and confirmed that it performs much better. Also, I reverted to my old C++ bracing style just to make Nish happy...hope you appreciate the sacrifice! Smile | :)

Change Log

Version 1.2

  • Updated for .NET Framework 1.1
  • Removed FirstCharacter and LastCharacter properties; Exclusions works across all characters
  • Replaced Password property with Generate() method
  • Removed PwdMaskFlags; use Exclusions property and/or ExcludeSymbols property
  • Used RNGCryptoServiceProvider instead of Random for random number generation
  • Updated demo application

Perfection (in design) is achieved not when there is nothing more to add, but rather when there is nothing more to take away. - Antoine de Saint-Exupéry

Version 1.1

  • Improved password generation algorithm

Version 1.0

  • Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Kevin Stewart
Web Developer
United States United States
Kevin is a software development manager for a small consumer-oriented company in SoHo, NY. He is technology and platform agnostic having worked on PCs, Macintosh and various forms of Unix and Linux. His programming knowledge includes several languages, including C/C++, Java and C#. In the rare moments when his head is not buried in the latest tech book purchase from Amazon, Kevin enjoys spending time with his wife Donna and their dog Kirby.

You may also be interested in...

Comments and Discussions

 
QuestionLicense for use of the above code Pin
monicanahar10-Apr-14 1:59
membermonicanahar10-Apr-14 1:59 
GeneralBug with pwdCharArray Pin
quandary2-Aug-12 5:29
memberquandary2-Aug-12 5:29 
GeneralNice work Pin
RockingDownTheHighway3-Feb-11 4:55
memberRockingDownTheHighway3-Feb-11 4:55 
QuestionBug in Generate() method Pin
geoffbishop@gmail.com15-Dec-08 9:48
membergeoffbishop@gmail.com15-Dec-08 9:48 
QuestionBug when exact password size requested Pin
geoffbishop@gmail.com15-Dec-08 9:09
membergeoffbishop@gmail.com15-Dec-08 9:09 
AnswerRe: Bug when exact password size requested Pin
Member 235774024-Feb-09 7:34
memberMember 235774024-Feb-09 7:34 
GeneralException thrown Pin
thom_ek29-May-08 2:49
memberthom_ek29-May-08 2:49 
GeneralPronounceable passwords Pin
marschills16-Nov-07 11:29
membermarschills16-Nov-07 11:29 
GeneralRe: Pronounceable passwords Pin
Kevin Stewart16-Nov-07 18:30
memberKevin Stewart16-Nov-07 18:30 
GeneralOverriding the constructor Pin
Spam Basket9-Mar-07 8:36
memberSpam Basket9-Mar-07 8:36 
GeneralHere's a VB Implementation... Pin
code-frog31-Jan-07 7:59
membercode-frog31-Jan-07 7:59 
GeneralRe: Here's a VB Implementation... Pin
Kevin Stewart7-Feb-07 14:28
memberKevin Stewart7-Feb-07 14:28 
GeneralRe: Here's a VB Implementation... Pin
code-frog8-Feb-07 2:38
membercode-frog8-Feb-07 2:38 
GeneralRe: Here's a VB Implementation... Pin
code-frog8-Feb-07 2:40
membercode-frog8-Feb-07 2:40 
GeneralI threw you a 5... Pin
code-frog30-Oct-06 18:43
membercode-frog30-Oct-06 18:43 
GeneralRe: I threw you a 5... Pin
Kevin Stewart3-Nov-06 2:02
memberKevin Stewart3-Nov-06 2:02 
QuestionCopyright Release? Pin
tgiphil4-Oct-05 18:14
membertgiphil4-Oct-05 18:14 
AnswerRe: Copyright Release? Pin
Kevin Stewart5-Oct-05 4:42
memberKevin Stewart5-Oct-05 4:42 
GeneralNice class Pin
Jan R Hansen17-May-05 20:46
memberJan R Hansen17-May-05 20:46 
GeneralRe: Nice class Pin
Danny Crowell27-Jun-05 12:11
memberDanny Crowell27-Jun-05 12:11 
GeneralpwdCharArray Pin
aztracker121-Mar-05 8:41
memberaztracker121-Mar-05 8:41 
GeneralInfinite Loop using RepeatCharacters as false Pin
frihani7-Feb-05 11:34
memberfrihani7-Feb-05 11:34 
GeneralAlternative Pin
smallguy16-Jun-04 8:04
membersmallguy16-Jun-04 8:04 
GeneralRe: Alternative Pin
Kevin Stewart16-Jun-04 8:30
memberKevin Stewart16-Jun-04 8:30 
GeneralRe: Alternative Pin
smallguy16-Jun-04 11:01
membersmallguy16-Jun-04 11:01 
GeneralRe: Alternative Pin
tomstrummer24-Aug-05 8:05
membertomstrummer24-Aug-05 8:05 
GeneralJust a quick validate for those who need it Pin
Dirk Vandenheuvel18-Feb-04 4:20
memberDirk Vandenheuvel18-Feb-04 4:20 
GeneralRe: Just a quick validate for those who need it Pin
Kevin Stewart16-Jun-04 8:32
memberKevin Stewart16-Jun-04 8:32 
GeneralGetCryptographicRandomNumber Pin
Anonymous8-Jan-04 5:15
sussAnonymous8-Jan-04 5:15 
GeneralRe: GetCryptographicRandomNumber Pin
Anonymous8-Jan-04 5:46
sussAnonymous8-Jan-04 5:46 
GeneralNEED URGENT EXPERTISE PLEASE. Pin
solaces25-Aug-03 5:33
membersolaces25-Aug-03 5:33 
GeneralSuggestion: ToCharArray() Pin
Arjan Einbu28-Jul-03 22:15
memberArjan Einbu28-Jul-03 22:15 
GeneralRe: Suggestion: ToCharArray() Pin
Kevin Stewart29-Jul-03 3:18
memberKevin Stewart29-Jul-03 3:18 
GeneralBias in Password Selection Pin
mdgray28-Jul-03 15:17
membermdgray28-Jul-03 15:17 
GeneralRe: Bias in Password Selection Pin
mdgray28-Jul-03 15:39
membermdgray28-Jul-03 15:39 
GeneralRe: Bias in Password Selection Pin
Jeffrey Sax28-Jul-03 17:50
memberJeffrey Sax28-Jul-03 17:50 
GeneralRe: Bias in Password Selection Pin
mdgray28-Jul-03 21:07
membermdgray28-Jul-03 21:07 
GeneralRe: Bias in Password Selection Pin
Kevin Stewart29-Jul-03 3:13
memberKevin Stewart29-Jul-03 3:13 
GeneralRe: Bias in Password Selection Pin
mdgray29-Jul-03 5:52
membermdgray29-Jul-03 5:52 
GeneralRe: Bias in Password Selection Pin
Kevin Stewart29-Jul-03 6:33
memberKevin Stewart29-Jul-03 6:33 
QuestionLatest version ? Pin
jfrandol28-Jul-03 5:24
memberjfrandol28-Jul-03 5:24 
AnswerRe: Latest version ? Pin
Kevin Stewart29-Jul-03 3:09
memberKevin Stewart29-Jul-03 3:09 
GeneralCryptographic random numbers Pin
Jeffrey Sax6-Jul-03 21:59
memberJeffrey Sax6-Jul-03 21:59 
GeneralRe: Cryptographic random numbers Pin
Kevin Stewart11-Jul-03 10:38
memberKevin Stewart11-Jul-03 10:38 
GeneralExclusion Option doesn't run [.NET FR 1.1] Pin
precchia4-Jul-03 3:26
memberprecchia4-Jul-03 3:26 
GeneralRe: Exclusion Option doesn't run [.NET FR 1.1] Pin
Kevin Stewart4-Jul-03 5:35
memberKevin Stewart4-Jul-03 5:35 
GeneralRe: Exclusion Option doesn't run [.NET FR 1.1] Pin
josh_heller17-Jul-03 6:52
memberjosh_heller17-Jul-03 6:52 
GeneralA question and another suggestion Pin
Domenic [Geekn]2-May-03 4:45
memberDomenic [Geekn]2-May-03 4:45 
GeneralRe: A question and another suggestion Pin
Kevin Stewart2-May-03 7:30
memberKevin Stewart2-May-03 7:30 
GeneralRe: A question and another suggestion Pin
Domenic [Geekn]2-May-03 16:16
memberDomenic [Geekn]2-May-03 16:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 18 Aug 2003
Article Copyright 2002 by Kevin Stewart
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid