I need to create some unique keys. GUID is one option as they give Globally Unique identifier but they are big. If you want to issue unique number in your application which you want to give as AppKey or any reference number then GUIDs is obviously not a solution.
RNG means Random Number Generator
The .net Framework provides RNGCryptoServiceProvider class which Implements a cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider (CSP). This class is usually used to generate random numbers.
Although we can use this class to generate unique number in some sense. Moreover while generating key we can make key more complicated by making it as alpha numeric rather than numeric only.
So, we used this function along with some character masking to generate unique key of any length (here I take 32 character long key).
Below is code sample:
private string RNGCharacterMask()
int maxSize = 32;
int minSize = 32;
char chars = new char;
a = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890″;
chars = a.ToCharArray();
int size = maxSize;
byte data = new byte;
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
size = maxSize;
data = new byte[size];
StringBuilder result = new StringBuilder(size);
foreach (byte b in data)
result.Append(chars[b % (chars.Length – 1)]);
Happy Coding… :)