Thursday, 20 March 2014

Generation of unique key in C# code

Generating the alphanumeric unique key in C# by passing the size  of unique key generations. Here you need importing the cryptography and passing the size of you unique key value. You can make it only alphabet unique value by changing the values by string str_a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; . using System.Security.Cryptography;  protected string GetUniqueKey(int maxSize)         {             //int int_minSize = 5;             char[] chars = new char[62]; //instantiate with character type array;             string str_a = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";             chars = str_a.ToCharArray();             int int_size = int_maxSize;             byte[] byte_data = new byte[1];             RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();             crypto.GetNonZeroBytes(byte_data);             int_size = int_maxSize;             byte_data = new byte[int_size];             crypto.GetNonZeroBytes(byte_data);             StringBuilder result = new StringBuilder(int_size);             foreach (byte b in byte_data)             {                 result.Append(chars[b % (chars.Length - 1)]);             }             return result.ToString();         }

No comments:

Post a Comment


You will like to read it