Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
private string GetUniqueKey()
  {
  int maxSize  = 8 ;
  int minSize = 5 ;
  char[] chars = new char[62];
  string a;
  a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  chars = a.ToCharArray();
  int size  = maxSize ;
  byte[] data = new byte[1];
  RNGCryptoServiceProvider  crypto = new RNGCryptoServiceProvider();
  crypto.GetNonZeroBytes(data) ;
  size =  maxSize ;
  data = new byte[size];
  crypto.GetNonZeroBytes(data);
  StringBuilder result = new StringBuilder(size) ;
  foreach(byte b in data )
  { result.Append(chars[__b % (chars.Length - )>); }
   return result.ToString();
  }
Posted
Updated 28-Jul-15 3:02am
v2
Comments
F-ES Sitecore 28-Jul-15 9:03am    
google "c# random characters" and try a different sample, that one looks a bit complicated for what it does.
Sinisa Hajnal 28-Jul-15 9:08am    
It doesn't work how? do you get an error? If so, which? Which line creates the error? If it is not an error, but it doesn't work in some other way, use Improve question link and provide more details.

See https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx[^]. Generate random numbers and use each one as an index into the string of acceptable characters:
string list = "abcdef ... etc"
int index = nextRandom % list.length // i.e. remainder
nextCharacter = list.at(index)
repeat.
 
Share this answer
 
Use LINQ,
It will generate the 5 digit random characters.
var chars ="BCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random = new Random();
var result = new string(
Enumerable.Repeat(chars, 5)
.Select(s => s[random.Next(s.Length)])
.ToArray());
 
Share this answer
 
Comments
Richard Deeming 2-Nov-15 11:24am    
This answer is plagiarised from this answer[^], which was plagiarised from StackOverflow[^].
C#
// use static classes and methods where possible
// Don't use System.Random it is not secure
private static readonly RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();

private static void Encode(char[] data, char[] lexicon)
{
    //Don't let null values break your code
    if (data == null)
    {
        throw new ArgumentException("data");
    }
    if (lexicon == null)
    {
        throw new ArgumentException("lexicon");
    }
    var randomNumber = new byte[1];
    int size = data.Length;
    //use char[] rather than a string.
    //Strings are not very secure as copies are made in memory each time a string  is changed
    for (int i = 0; i < size; i++)
    {
        //Load random number into buffer.
        //Use methods that return 'void' where possible.
        rngCsp.GetBytes(randomNumber);
        int index = randomNumber[0];
        data[i] = lexicon[index % lexicon.Length];

    }

}

private static void Main(string[] args)
{
    string lexicon = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    int size = 8;
   var data = new char[size];
    Encode(data,lexicon.ToCharArray());
    string password = new string(data);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900