Link to home
Start Free TrialLog in
Avatar of MrKevorkian
MrKevorkian

asked on

unique key generator

Hi i want to generate a unique key.
I want it to be made up of letters and numbers.

I have been looking at RC2 using the following code:

-----------------------------------
using System.Security.Cryptography;

static void Main(string[] args)
{
RC2 r = RC2.Create();
r.GenerateIV();
r.GenerateKey();

for (short i = 13; i < 200; i += 13)
{
byte[] data = BitConverter.GetBytes(i);
byte[] enc = r.CreateEncryptor().TransformFinalBlock(data, 0, 2);
Console.WriteLine("{0}\t= {1}", 13, Convert.ToBase64String(enc));
}
Console.ReadLine();
}

---------------------------------------------

The problem is that this generates a key that contains non alphanumeric characters:
i.e
/MQpLW6J+3o=  

----------------------------

so is there anything out there that will generate a unique alphanumeric key (Not a guid) about 10-12 characters.  something that will be unique across time.

thanks very much.
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

What's wrong with a GUID?...you could just remove the hypens and use Substring() to get the required number of characters from the left, middle, or right side...
Avatar of MrKevorkian
MrKevorkian

ASKER

if i remove characters - whats the guarentee that it will be unique?
i have had a look at RNGCryptoServiceProvider and that seems that it might be useful.  But doesnt  allow you to introduce a seed to it initially.
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Edit:
To achieve uniqueness, i would just store the numbers in the DB like suggested or use a (counter * key + counter) that will allways reach the desiered target-Number length. (e.g. if you know you need to generate "millions" then dont start the counter at "1" but use 1000000 as start for your counter.

The merging of those numbers could be more complex, depending on your need for this key.... But it will allow a unique number with each counter value, and it can be reproduced which counter was used to generate the key...