Generating the Random Key in c#
The RandomNumber class defined in the .NET Framework class library provides functionality
to generate random key. Create the static class with name KeyGenerator. Declare
the static readonly three char arrays. Define the static method GenerateRandomKey
with parameters minimum length for random key, maximum length for random key,
in random key weather allow the characters, allow the numbers and allow the symbols.
If you pass the false as parameter for allownumbers in the random key does not
have the numbers.
The KeyGenerator having the two overload methods. One takes no parameter and one
has parameters. RandomNumber class is having the Next method. Next method takes
maximum limit for the random key.
Following the code return the random key.
Enum CharacterType for random key.
enum CharacterType
{
UpperCase,
LowerCase,
Number,
Special
}
public static class KeyGenerator
{
private static readonly char[] _Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private static readonly char[] _Numbers = "1234567890".ToCharArray();
private static readonly char[] _Symbols = "!@#$%^&*.?".ToCharArray();
public static string GenerateRandomKey(int minimumLength, int maximumLength,
bool allowCharacters, bool allowNumbers, bool allowSymbols)
{
string[] _CharacterTypes;
_CharacterTypes = getCharacterTypes(allowCharacters, allowNumbers, allowSymbols);
StringBuilder randomKey = new StringBuilder(maximumLength);
int currentRandomKeyLength = RandomNumber.Next(maximumLength);
if (currentRandomKeyLength < minimumLength)
{
currentRandomKeyLength = minimumLength;
}
//Generate the randomKey
for (int i = 0; i < currentRandomKeyLength; i++)
{
randomKey.Append(getCharacter(_CharacterTypes));
}
return randomKey.ToString();
}
public static string GenerateRandomKey()
{
return GenerateRandomKey(10, 10, true, true, true);
}
// Getting character types allowed in the key //(UpperCase,LowerCase,Number,Special)
//Parameters
//Whether to allow characters
//Whether to allow numbers
//Whether to allow symbols
//Return type as string array.
private static string[] getCharacterTypes(bool allowCharacters, bool allowNumbers, bool allowSymbols)
{
ArrayList alCharacterTypes = new ArrayList();
foreach (string characterType in Enum.GetNames(typeof(CharacterType)))
{
CharacterType currentType =
(CharacterType)Enum.Parse(typeof(CharacterType),
characterType, false);
bool addType = false;
switch (currentType)
{
case CharacterType.LowerCase:
addType = allowCharacters;
break;
case CharacterType.Number:
addType = allowNumbers;
break;
case CharacterType.Special:
addType = allowSymbols;
break;
case CharacterType.UpperCase:
addType = allowCharacters;
break;
}
if (addType)
{
alCharacterTypes.Add(characterType);
}
}
return (string[])alCharacterTypes.ToArray(typeof(string));
}
// Getting character type randomly from the array of character types
//Parameter is Array of allowed character types.
// One of the types as string
private static string getCharacter(string[] characterTypes)
{
string characterType =
characterTypes[RandomNumber.Next(characterTypes.Length)];
CharacterType typeToGet =
(CharacterType)Enum.Parse(typeof(CharacterType), characterType, false);
switch (typeToGet)
{
case CharacterType.LowerCase:
return _Letters[RandomNumber.Next(_Letters.Length)].ToString().ToLower();
case CharacterType.UpperCase:
return _Letters[RandomNumber.Next(_Letters.Length)].ToString().ToUpper();
case CharacterType.Number:
return _Numbers[RandomNumber.Next(_Numbers.Length)].ToString();
case CharacterType.Special:
return _Symbols[RandomNumber.Next(_Symbols.Length)].ToString();
}
return null;
}
}