Solved

unique key generator

Posted on 2007-03-26
6
1,176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

729 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