Generating random passwords

Discussion in 'ASP .Net Security' started by Zhwgnon Flrq, Dec 23, 2003.

  1. Zhwgnon Flrq

    Zhwgnon Flrq Guest

    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.
     
    Zhwgnon Flrq, Dec 23, 2003
    #1
    1. Advertisements

  2. Zhwgnon Flrq

    amit agarwal Guest

    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;
    }
     
    amit agarwal, Dec 30, 2003
    #2
    1. Advertisements

  3. Zhwgnon Flrq

    Zhwgnon Flrq Guest

    Amit:

    The code you sent did not compile. Anyways, I wrote a similar function (a
    lot simplified though) to generate a random password of length 8.

    // This function generates a random alphanumeric password of length 8.
    private string GenerateRandomPassword ()
    {
    const string alphabets = "abcdefghijklmnopqrstuvwxyz";
    const string digits ="0123456789";
    StringBuilder password = new StringBuilder (8);
    Random rng = new Random ();
    int n = 0;

    for (int i = 0; i < 8; ++i)
    {
    if (i % 2 == 0)
    {
    n = rng.Next (26);
    password.Append (alphabets [n]);
    }
    else
    {
    n = rng.Next (10);
    password.Append (digits [n]);
    }
    }

    return password.ToString ();
    }

    Thanks.
     
    Zhwgnon Flrq, Dec 30, 2003
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.