Everyone Is An Achiever

Sunday, December 14, 2014

Generate strong password

6:28 AM Posted by Unknown No comments


Hello everyone, In this tutorial, we will learn how to generate a password that is safe and hard to detect. The methodology that we will follow for generating a safe password is: we will run a loop and for every time the loop runs we will generate a character and then append it to a string variable. Here I'll generate a 13 characters password consisting of lowercase and uppercase characters, numbers, and special characters. The question is, in every iteration how do we make the decision of generating a character? We will do that with the help of a random variable. The random variable will generate a number and we will compare this number against if else statement block, and if it matches with one of the blocks then we generate the corresponding character RANDOMLY. We will start with declaring the variables we're going to need:
            int randomNumber = 0;
            string password = "";
the randomNumber will be the variable that will hold the decision number. And the password variable is the one that will hold the password. Then we will create a random object:
            Random rnd = new Random();
rnd will generate a number and assign it to randomNumber. After that we write the loop that will generate the password.
for (int i = 1; i <= 13; i++)
            {
                randomNumber = rnd.Next(1,5);
                if (randomNumber == 1)
                    password += rnd.Next(1, 10);
                else if (randomNumber == 2)
                    password += Convert.ToChar(rnd.Next(65, 92));
                else if (randomNumber == 3)
                    password += Convert.ToChar(rnd.Next(97, 123));
                else if (randomNumber == 4)
                    password += Convert.ToChar(rnd.Next(33, 46));
            }

  • randomNumber = rnd.Next(1, 5); will generate an integer number between 1 and 5 (5 is not included) and will assign it to randomNumber. 
  • Then we will check the value of randomNumber against if else block. If randomNumber ==1 then we will generate a number between 1 and 9
  • if randomNumber ==2 we will generate a number between 65 and 91 and as you know these numbers represent the corresponding ASCII values for uppercase alphabets A-Z then we will convert it to a character using Convert.ToChar() method
  • If randomNumber ==3 we will generate a lowercase character a-z and lastly if randomNumber ==4 will generate one of these special characters !"#$%'()*+/- 


here is the full code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace TestConsole
{

    class Program
    {
        static void Main(string[] args)
        {

            int randomNumber = 0;
            string password = "";
            Random rnd = new Random();
            for (int i = 1; i <= 13; i++)
            {
                randomNumber = rnd.Next(1,5);
                if (randomNumber == 1)
                    password += rnd.Next(1, 10);
                else if (randomNumber == 2)
                    password += Convert.ToChar(rnd.Next(65, 91));
                else if (randomNumber == 3)
                    password += Convert.ToChar(rnd.Next(97, 123));
                else if (randomNumber == 4)
                    password += Convert.ToChar(rnd.Next(33, 46));
            }

            Console.WriteLine("Your new password is {0}", password);
           
            Console.ReadLine();
        }
    }
}

output:

A typical procedure to change the user's password:
  1.  If the user demanded a new password using his email, first check if the email is exist in the database 
  2.  If the email is valid: 
  •  Generate a new password 
  •  Encrypt it by converting it to a hash digest 
  •  Replace the hash digest (of the new password) with the old one in the database 
  •  Send the new password to the user

0 comments:

Post a Comment