Saturday, 13 August 2011

C-Sharp Dot Net Code


using System;

namespace randomPassword
{
   
    class Program
    {
       
        static void Main(string[] args)
        {
            // Gets the length  of the password.
            int len;

            // Gets the type of the password
            int sel;

            // Gets a random value.
            int rand;

            // The string that contains the password.
            string pass = "";

            // Random number.
            Random num = new Random();

            // Get the password length (user input).
            Console.WriteLine("Password length:");
            len = Convert.ToInt32(Console.ReadLine());

            // Get the password type (user input).
            Console.WriteLine("Select password type:");
            Console.WriteLine("1 - Letters, Symbols, Numbers");
            Console.WriteLine("2 - Letters, Symbols");
            Console.WriteLine("3 - Letters, Numbers");
            Console.WriteLine("4 - Letters");
            Console.WriteLine("5 - Numbers");
            sel = Convert.ToInt32(Console.ReadLine());
           
            // Based on the password type, generate a password.
            switch (sel)
            {
                    // Complex password (letters, numbers, symbols).
                case 1:
                    for (int c1 = 0; c1 < len; c1++)
                        pass += char.ConvertFromUtf32(num.Next(33, 126));
                    break;

                    // Password composed of letters and symbols.
                case 2:
                    for (int c1 = 0; c1 < len; c1++)
                    {
                    genNew:rand = num.Next(33, 126);
                        if ((rand < 48) || (rand > 57))
                        {
                            pass += char.ConvertFromUtf32(rand);
                        }
                        else
                        {
                            goto genNew;
                        }
                    }
                    break;

                    // Password composed of letters and numbers.
                case 3:
                    for (int c1 = 0; c1 < len; c1++)
                    {
                    genNew2:
                        rand = num.Next(33, 126);
                        if (((rand > 47) && (rand < 58)) || ((rand < 122) && (rand > 96)) || ((rand < 89) && (rand > 64)))
                        {
                            pass += char.ConvertFromUtf32(rand);
                        }
                        else
                        {
                            goto genNew2;
                        }
                    }
                    break;

                    // Password composed of letters.
                case 4:
                    for (int c1 = 0; c1 < len; c1++)
                    {
                    genNew3:
                        rand = num.Next(33, 126);
                        if (((rand > 65) && (rand < 90)) || ((rand < 122) && (rand > 96)))
                        {
                            pass += char.ConvertFromUtf32(rand);
                        }
                        else
                        {
                            goto genNew3;
                        }
                    }
                    break;

                    // Password composed of numbers.
                case 5:
                    for (int c1 = 0; c1 < len; c1++)
                    {
                        pass += char.ConvertFromUtf32(num.Next(48, 57));
                    }
                    break;
            }

            // Show the generated password.
            Console.WriteLine("Your password is: {0}", pass);
            Console.ReadLine();

        }
    }
}