Click here to Skip to main content
11,930,069 members (56,472 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


24 bookmarked

Simple Password Generator

, 18 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple password generator, based on alphabets


Sometimes you need to create words from a given alphabet, so I'd like to share a very simple solution. The solution is scalable, so you can add your own alphabet. Let's take a look at the code.

Using the Code

The code consists of several classes:

  • AlphabetDictionaries - predefined alphabets
  • AlphabetWordGenerator - generate word accordingly alphabet

public static class AlphabetDictionaries
    public const string EnglishLowerCase = "abcdefghijklmnopqrstuvwxyz";
    public const string EnglishUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public const string Numeric = "0123456789";
    public const string SpecialSymbols = "`~!@#$%^&*()-_=+[]{}\\|;:'\",<.>/?";

Here's main and only one class - AlphabetWordGenerator

public sealed class AlphabetWordGenerator : IWordGenerator
    private readonly string _alphabet;
    private readonly RNGCryptoServiceProvider _provider = new RNGCryptoServiceProvider();

    public AlphabetWordGenerator(ICollection<string> alphabets)
        if (alphabets == null || alphabets.Count == 0)
            throw new ArgumentException("Alphabets is null or empty");
        if (alphabets.Any(string.IsNullOrWhiteSpace))
            throw new ArgumentException("Alphabet should not be ampty");
        _alphabet = string.Join(string.Empty, alphabets);

    public AlphabetWordGenerator(string alphabet)
        : this(new[] { alphabet })

    public string Generate(int wordLength)
        if (wordLength < 1)
            throw new IndexOutOfRangeException("wordLength is out of range");
        return DoGenerate(wordLength);

    private string DoGenerate(int wordLength)
        var repository = new List<char>(wordLength);
        var data = new byte[wordLength];
        foreach (byte item in data)
            int index = Convert.ToInt32(item)%_alphabet.Length;
            char symbol = _alphabet[index];
        return string.Join(string.Empty, repository);

Let's create a word with lowercase English letters:

var generator = new AlphabetWordGenerator(AlphabetDictionaries.EnglishLowerCase);
var password = generator.Generate(10); 

Result password is equal to uddfxjfbxp 

Another example with several alphabets:

var alphabets = new List<string>

var generator = new AlphabetWordGenerator(alphabets);
var password = generator.Generate(40);    

Result password is equal to [3xH(kzizo@51m@q}qpIYq[CgUBbjKf^3E@JF^T;


  • 26th July, 2012: Initial version
  • 20th December, 2013: Length's been moved to generate method


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sergey Morenko
Software Developer (Senior)
United States United States
B.Sc. in Computer Science.

You may also be interested in...

Comments and Discussions

SuggestionDoes what it says Pin
John Brett19-Dec-13 5:30
memberJohn Brett19-Dec-13 5:30 
GeneralRe: Does what it says Pin
Sergey Morenko20-Dec-13 10:06
professionalSergey Morenko20-Dec-13 10:06 
Question[My vote of 2] 2 Easier Way Pin
Kevin Marois26-Jul-12 8:01
memberKevin Marois26-Jul-12 8:01 
AnswerRe: [My vote of 2] 2 Easier Way Pin
GSerjo26-Jul-12 8:18
memberGSerjo26-Jul-12 8:18 
GeneralRe: [My vote of 2] 2 Easier Way Pin
Kevin Marois26-Jul-12 8:27
memberKevin Marois26-Jul-12 8:27 
Your example produced a ramdon password. GetRandomFileName will do the same.

Using my techniques doesn't require to set up an 'alphabet'.

How does that make me wrong?
If it's not broken, fix it until it is

GeneralRe: [My vote of 2] 2 Easier Way Pin
GSerjo26-Jul-12 8:40
memberGSerjo26-Jul-12 8:40 
GeneralRe: [My vote of 2] 2 Easier Way Pin
Caner Korkmaz26-Jul-12 9:33
memberCaner Korkmaz26-Jul-12 9:33 
GeneralMy vote of 5 Pin
zhanazhan25-Jul-12 18:50
memberzhanazhan25-Jul-12 18:50 
GeneralRe: My vote of 5 Pin
GSerjo25-Jul-12 20:18
memberGSerjo25-Jul-12 20:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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.151126.1 | Last Updated 18 Dec 2013
Article Copyright 2012 by Sergey Morenko
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid