Generating random passwords

From: amit agarwal (amit_agarwal_at_idealake.com)
Date: 12/30/03


Date: Mon, 29 Dec 2003 22:32:41 -0800


>-----Original Message-----
>Hello all:
>
>I am trying to build a standar Web registration screen
and was wondering of
>what algo to use to generate a random password for users.
>
>Any kind of help would be greatly appreciated.
>
>Thanks.
>
>
>.

This article shows how to generate a random alpha-numeric
password for an online user-registration. Calling the
function GeneratePassword() will return a alpha-numeric
string of 10 digits (in this example) which can be sent as
a system generated password to the user and the relevant
record in the database. To generate a particular digit
password, change the 10 in GeneratePassword() function to
your choice.
private string GetPassword(int nLength)
{
  int i;
  double nRnd;
  Random oRandom;
  string c="";
  int a, b, d;
  bool bMadeConsonant = false;
  const string strAllConsonants="bcdfghjklmnpqrstvwxyz";
  const string strVocal = "aeiou";
  const string intnum="0123456789";
  string sPassword = "";
  for(i=0;i < nLength;i++)
  {
    oRandom = new Random();
    nRnd = oRandom.NextDouble();
    if(sPassword != "" && !bMadeConsonant && (nRnd < 0.30))
    {
      a = Convert.ToInt16(intnum.Length *
oRandom.NextDouble()) + 1;
      c = intnum.Substring(a,1);
      c = c + c;
      i = i + 1;
      bMadeConsonant = true;
    }
    else
    {
      if(!bMadeConsonant && (nRnd< 0.60))
      {
        try
        {
          b = Convert.ToInt16(strAllConsonants.Length *
oRandom.NextDouble()) + 1;
          c = strAllConsonants.Substring(a, 1);
          bMadeConsonant = true;
        }
        catch(ArgumentOutOfRangeException)
        {}
      }
      else
      {
        try
        {
          d = Convert.ToInt16(strVocal.Length *
oRandom.NextDouble()) + 1;
          c = strVocal.Substring(d, 1);
          bMadeConsonant = false;
        }
        catch(ArgumentOutOfRangeException)
        {}
      }
    }
    sPassword += c;
  }
  if(sPassword.Length > nLength)
    sPassword = sPassword.Substring(0,nLength);
  return sPassword;
}

public string GeneratePassword()
{
  string sPasswd=GetPassword(10);
  while(sPasswd=="")
    sPasswd=GetPassword(10);
  return sPasswd;
}

 

>



Relevant Pages