Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
i want c# code for keygenerator that can generate keys of 16 digits in alphanumeric characters
Posted

1 solution

The class :

C#
class KeyGenerator
{
    public KeyGenerator()
    {
        chars = new List<char>();
        generated = new List<string>();
        for (int i = 0; i < 10; i++)
            chars.Add((char)('0' + i));
        for (char c = 'A'; c <= 'Z'; c++)
            chars.Add(c);
        random = new Random();
    }

    private List<char> chars;
    private List<string> generated;
    Random random;
    private List<string> keys;
    public string GetNext(int len)
    {
        StringBuilder result = new StringBuilder();
        bool duplicate = true;
        while(duplicate)
        {
            result.Clear();
            for (int i = 0; i < len; i++)
            {
                int index = random.Next(chars.Count);
                result.Append(chars[index]);
            }
            if (!generated.Contains(result.ToString()))
                duplicate = false;
        }
        return result.ToString();
    }
}


Its usage is :

C#
KeyGenerator keyGenerator = new KeyGenerator();
for (int i = 0; i < 5; i++)
   Console.WriteLine(keyGenerator.GetNext(16));


Hope it helps.
 
Share this answer
 
Comments
[no name] 26-Dec-11 8:07am    
At least because of the expenses of this answer my 5.
Regards
Amir Mahfoozi 26-Dec-11 8:26am    
Thank you.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900