Solved

unique key generator

Posted on 2007-03-26
6
1,170 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:MrKevorkian
  • 2
  • 2
  • 2
6 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18793961
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...
0
 
LVL 1

Author Comment

by:MrKevorkian
ID: 18798505
if i remove characters - whats the guarentee that it will be unique?
0
 
LVL 1

Author Comment

by:MrKevorkian
ID: 18798529
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.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 total points
ID: 18800168
If you want to GUARANTEE that your number is unique then you would have to store every single number you've ever used in a database so you can search for previously used values.

Otherwise I'm pretty sure it doesn't matter what system you use to generate your "unique" key as you are at best simply "not likely" to get a repeat value.

But I'm no security expert...
0
 
LVL 11

Accepted Solution

by:
rdrunner earned 300 total points
ID: 18814269
Hello...

Du you want to include any other informations in your "key" or should it "just" be a number?

What you could do is to create a small function that will convert several bytes into a set of allowed characters.  This could be done along the lines of mime encoding. So basically you get your number as a byte array, then you take small chunks of it (in32 Values) and mod them through the number of allowed characters (This gets the remainder) and pick the nth Character out of your allowed set.. then you (Integer) divide them through the number of allowed chars and repeat the process until you are left with a '0'

To revert you can reverse the process and retrieve the number again.

Also this allows you to decide which letters are skipped (for example I/1 or O/0), so there wont be any characters that are hard to read...
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 18814296
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...
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question