Solved

unique key generator

Posted on 2007-03-26
6
1,167 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

912 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now