Friday, December 19, 2014

PROGRAMMING: C# CODE TO GENERATE RANDOM PASSWORDS DEVELOPER TUTORIAL

C# CODE TO GENERATE RANDOM PASSWORDS - DEVELOPED BY JAY STILLA + CO. MISTERBOTS



Here is a very useful C# code snippet for generating random strong passwords. The password is generated using upper case and lower case alphabets, numerals and special characters.


//create constant strings for each type of characters
static string alphaCaps = "QWERTYUIOPASDFGHJKLZXCVBNM";
static string alphaLow = "qwertyuiopasdfghjklzxcvbnm";
static string numerics = "1234567890";
static string special = "@#$";
//create another string which is a concatenation of all above
string allChars = alphaCaps + alphaLow + numerics + special;
Random r = new Random();
public string GenerateStrongPassword(int length)
{
    String generatedPassword = "";
    if (length < 4)
        throw new Exception("Number of characters should be greater than 4.");
    // Generate four repeating random numbers are postions of
    // lower, upper, numeric and special characters
    // By filling these positions with corresponding characters,
    // we can ensure the password has atleast one
    // character of those types
    int pLower, pUpper, pNumber, pSpecial;
    string posArray = "0123456789";
    if (length < posArray.Length)
        posArray = posArray.Substring(0, length);
    pLower = getRandomPosition(ref posArray);
    pUpper = getRandomPosition(ref posArray);
    pNumber = getRandomPosition(ref posArray);
    pSpecial = getRandomPosition(ref posArray);
    for (int i = 0; i < length; i++)
    {
        if (i == pLower)
            generatedPassword += getRandomChar(alphaCaps);
        else if (i == pUpper)
            generatedPassword += getRandomChar(alphaLow);
        else if (i == pNumber)
            generatedPassword += getRandomChar(numerics);
        else if (i == pSpecial)
            generatedPassword += getRandomChar(special);
        else
            generatedPassword += getRandomChar(allChars);
    }
    return generatedPassword;
}
private string getRandomChar(string fullString)
{
    return fullString.ToCharArray()[(int)Math.Floor(r.NextDouble() * fullString.Length)].ToString();
}
private int getRandomPosition(ref string posArray)
{
    int pos;
    string randomChar = posArray.ToCharArray()[(int)Math.Floor(r.NextDouble()
                                   * posArray.Length)].ToString();
    pos = int.Parse(randomChar);
    posArray = posArray.Replace(randomChar, "");
    return pos;
}

3 comments:

  1. Buildabotshop.BlogSpot.Com This Blog is For Intellectual People Who Enjoy Learning New Knowledge And Sharing It With Others Open Source, Public Domain Empowering Light Seekers Of Advance knowledge and Tools And Multiple Levels Realms and Domains

    ReplyDelete
  2. learn coding for kids Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info.

    ReplyDelete

Feel Free To Request Any Content You Would Be Interested In Seeing Featured On TopBots.BlogSpot.Com This Blog is For Intellectual People Who Enjoy Learning New Knowledge And Sharing It With Others Open Source, Public Domain Empowering Light Seekers Of Advance knowledge and Tools And Multiple Levels Realms and Domains. If Would Like To Join The Team And Become A Blog Editor and Writer Feel Free To Request Information Below: Comment Whatever You Desire Its A Free Globe Last Time I Checked. Enjoy and Please Help By Subscribing And Re posting.