Solved

unique key generator

Posted on 2007-03-26
6
1,166 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.NET Error 7 42
.Net Web Site Password specs 2 21
Check if number is currency 15 29
Open a word document 23 15
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

759 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

16 Experts available now in Live!

Get 1:1 Help Now