C++ | Generate password and test password strength.

Hello peeps! Lord Hypersonic greets you. Today I am sharing the source code of c++ language. This program has two features.
The first feature generates password(s) for you. First, it will ask you for the length of passwords you want to generate, then it will ask for the number of passwords to generate. Then it will ask for different character sets which it will use to generate passwords. You can also use any combo of them. It uses different cases, just like sample space set in probability, total 16 cases are there.
Second feature test password strength. First, it will ask you to provide a password, while you typing your password, it will be hidden with "*". You can also remove the characters while typing by pressing backspace. Then it will first check the length of the password. If it's length is less than 8 then the password is very weak / Too short. And if the length is greater than equals to 8, it will check the presence of lower case alpha, uppercase alpha, numeric values, and symbols and rate the password according to it. 
Also, I try to make the program a well documented, so if you do not understand some part of code then refer to the comments and if still, you have difficulties then join my discord server from the link given at right side and ask me there. 😊


Source Code:-

/*

Program: Password GnT
Description: This program can generate password(s) for you and can also test the strength of the password.
             While generating it will ask you the length of password and number of passwords to generate and ask you to apply character set of your choice.
             While Testing strength it will keep your password hidden.
Author : Lord Hypersonic
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blogspot.in

*/

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

using namespace std;

//character array for different character sets.
char lower[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char upper[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char num[]={'1','2','3','4','5','6','7','8','9','0'};
char special[]={'`','~','!','@','#','$','%','^','&','*','(',')','_','-','+','\\','|',']','}','[','{','\'','\"',';',':','?','/','.','>',',','<'};

//function to generate passwords.
void PGenerator(int length, int numbers, char ch[4])
{
    string pass[numbers]; //array of string for number of passwords.
    srand(time(0)); //seeding random numbers.
    int c1,c2,c3,c4;
    //if user didn't select any character set then it will generate password with lower case alpha by default.
    if(((ch[0]=='n' || ch[0]=='N') && (ch[1]=='n' || ch[1]=='N') && (ch[2]=='n' || ch[2]=='N') && (ch[3]=='n' || ch[3]=='N')) || ((ch[0]=='y' || ch[0]=='Y') && (ch[1]=='n' || ch[1]=='N') && (ch[2]=='n' || ch[2]=='N') && (ch[3]=='n' || ch[3]=='N')))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%25)+0;  //generating random numbers.
                pass[j].push_back(lower[c1]); //putting random number in pass array
            }
        }
    }
    // if user select only special characters to generate password.
    if((ch[0]=='n' || ch[0]=='N') && (ch[1]=='n' || ch[1]=='N') && (ch[2]=='n' || ch[2]=='N') && (ch[3]=='y' || ch[3]=='Y'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%30)+0;
                pass[j].push_back(special[c1]);
            }
        }
    }
    //if user select numbers for creating password.
    if((ch[0]=='n' || ch[0]=='N') && (ch[1]=='n' || ch[1]=='N') && (ch[2]=='y' || ch[2]=='Y') && (ch[3]=='n' || ch[3]=='N'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%9)+0;
                pass[j].push_back(num[c1]);
            }
        }
    }
    //if user select upper case characters only for password generation.
    if((ch[0]=='n' || ch[0]=='N') && (ch[1]=='y' || ch[1]=='Y') && (ch[2]=='n' || ch[2]=='N') && (ch[3]=='n' || ch[3]=='N'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%25)+0;
                pass[j].push_back(upper[c1]);
            }
        }
    }
    //if user select lower case alpha and special characters for password generation.
    if((ch[0]=='y' || ch[0]=='Y') && (ch[1]=='n' || ch[1]=='N') && (ch[2]=='n' || ch[2]=='N') && (ch[3]=='y' || ch[3]=='Y'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%25)+0;
                c2=(rand()%30)+0;
                int c=(rand()%2)+1; //generating random numbers between 1 to 2 for selecting which character to insert in pass.
                if(c==1) pass[j].push_back(lower[c1]);
                if(c==2) pass[j].push_back(special[c2]);
            }
        }
    }
    //if user select lower case alpha and numbers for generating password.
    if((ch[0]=='y' || ch[0]=='Y') && (ch[1]=='n' || ch[1]=='N') && (ch[2]=='y' || ch[2]=='Y') && (ch[3]=='n' || ch[3]=='N'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%25)+0;
                c2=(rand()%9)+0;
                int c=(rand()%2)+1;
                if(c==1) pass[j].push_back(lower[c1]);
                if(c==2) pass[j].push_back(num[c2]);
            }
        }
    }
    //if user select lower and upper case alpha to generate password.
    if((ch[0]=='y' || ch[0]=='Y') && (ch[1]=='y' || ch[1]=='Y') && (ch[2]=='n' || ch[2]=='N') && (ch[3]=='n' || ch[3]=='N'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers;j++)
            {
                c1=(rand()%25)+0;
                c2=(rand()%25)+0;
                int c=(rand()%2)+1;
                if(c==1) pass[j].push_back(lower[c1]);
                if(c==2) pass[j].push_back(upper[c2]);
            }
        }
    }
    //if user select upper case alpha and special characters
    if((ch[0]=='n' || ch[0]=='N') && (ch[1]=='y' || ch[1]=='Y') && (ch[2]=='n' || ch[2]=='N') && (ch[3]=='y' || ch[3]=='Y'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%25)+0;
                c2=(rand()%30)+0;
                int c=(rand()%2)+1;
                if(c==1) pass[j].push_back(upper[c1]);
                if(c==2) pass[j].push_back(special[c2]);
            }
        }
    }
    //if user select number and special characters to generate password.
    if((ch[0]=='n' || ch[0]=='N') && (ch[1]=='n' || ch[1]=='N') && (ch[2]=='y' || ch[2]=='Y') && (ch[3]=='y' || ch[3]=='Y'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%30)+0;
                c2=(rand()%9)+0;
                int c=(rand()%2)+1;
                if(c==1) pass[j].push_back(special[c1]);
                if(c==2) pass[j].push_back(num[c2]);
            }
        }
    }
    //if user select upper case alpha and numbers to generate password.
    if((ch[0]=='n' || ch[0]=='N') && (ch[1]=='y' || ch[1]=='Y') && (ch[2]=='y' || ch[2]=='Y') && (ch[3]=='n' || ch[3]=='N'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%25)+0;
                c2=(rand()%9)+0;
                int c=(rand()%2)+1;
                if(c==1) pass[j].push_back(upper[c1]);
                if(c==2) pass[j].push_back(num[c2]);
            }
        }
    }
    //if user select upper case alpha numbers and special characters for generating passwords.
    if((ch[0]=='n' || ch[0]=='N') && (ch[1]=='y' || ch[1]=='Y') && (ch[2]=='y' || ch[2]=='Y') && (ch[3]=='y' || ch[3]=='Y'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%25)+0;
                c2=(rand()%9)+0;
                c3=(rand()%30)+0;
                int c=(rand()%3)+1;
                if(c==1) pass[j].push_back(upper[c1]);
                if(c==2) pass[j].push_back(num[c2]);
                if(c==3) pass[j].push_back(special[c3]);
            }
        }
    }
    //if user select lower case alpha, numbers and special characters to generate passwords.
    if((ch[0]=='Y' || ch[0]=='y') && (ch[1]=='n' || ch[1]=='N') && (ch[2]=='y' || ch[2]=='Y') && (ch[3]=='y' || ch[3]=='Y'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%25)+0;
                c2=(rand()%9)+0;
                c3=(rand()%30)+0;
                int c=(rand()%3)+1;
                if(c==1) pass[j].push_back(lower[c1]);
                if(c==2) pass[j].push_back(num[c2]);
                if(c==3) pass[j].push_back(special[c3]);
            }
        }
    }
    //if user select lower and upper case alpha and special characters to generate passwords.
    if((ch[0]=='y' || ch[0]=='Y') && (ch[1]=='y' || ch[1]=='Y') && (ch[2]=='n' || ch[2]=='N') && (ch[3]=='y' || ch[3]=='Y'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%25)+0;
                c2=(rand()%25)+0;
                c3=(rand()%30)+0;
                int c=(rand()%3)+1;
                if(c==1) pass[j].push_back(upper[c1]);
                if(c==2) pass[j].push_back(lower[c2]);
                if(c==3) pass[j].push_back(special[c3]);
            }
        }
    }
    //if user select lower and upper case alpha and numbers to generate passwords.
    if((ch[0]=='y' || ch[0]=='Y') && (ch[1]=='y' || ch[1]=='Y') && (ch[2]=='y' || ch[2]=='Y') && (ch[3]=='n' || ch[3]=='N'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%25)+0;
                c2=(rand()%9)+0;
                c3=(rand()%25)+0;
                int c=(rand()%3)+1;
                if(c==1) pass[j].push_back(upper[c1]);
                if(c==2) pass[j].push_back(num[c2]);
                if(c==3) pass[j].push_back(lower[c3]);
            }
        }
    }
    if((ch[0]=='y' || ch[0]=='Y') && (ch[1]=='y' || ch[1]=='Y') && (ch[2]=='y' || ch[2]=='Y') && (ch[3]=='y' || ch[3]=='Y'))
    {
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<numbers; j++)
            {
                c1=(rand()%25)+0;
                c2=(rand()%9)+0;
                c3=(rand()%30)+0;
                c4=(rand()%25)+0;
                int c=(rand()%4)+1;
                if(c==1) pass[j].push_back(upper[c1]);
                if(c==2) pass[j].push_back(num[c2]);
                if(c==3) pass[j].push_back(special[c3]);
                if(c==4) pass[j].push_back(lower[c4]);
            }
        }
    }
    cout<<endl;
    for(int i=0; i<numbers; i++)  //printing passwords.
        cout<<i+1<<". \t"<<pass[i]<<endl;
}

//function for testing password strength.
void PTester()
{
    string password,P,Strength;
    char p;
    cout<<"Enter Password: ";
    p=_getch();
    while(p!=13) //hiding password while typing. Showing *
    {
        if(p==8) //if user press backspace
        {
            P.resize(P.length()-1); //reduce the *'s by one
            cout<<P; //printing *'s
            password.resize(password.length()-1); //reducing password by 1
        }
        else { //otherwise
            P=P+"*"; //add star to the P
            cout<<P; //print *
            password.push_back(p); // add character pressed by use to password.
        }
        p=_getch();
        system("cls");
        cout<<"Enter Password: ";
    }
    if(password.length()<8)  //if length of password is less than 8, then password is very weak and short.
        Strength="Very Weak / Too Short";
    if(password.length()>=8) // is password is greater than or equal to 8
    {
        int Lower=0,Upper=0,Num=0,Sym=0;
        for(unsigned int i=0; i<password.length(); i++)
        {
            if(islower(password[i])) Lower=1;  //if password contain lower case alpha then set Lower to 1
            else if (isupper(password[i])) Upper=1; //if password contain upper case alpha then set Upper to 1
            else if(isdigit(password[i])) Num=1; // if password contain numbers then set Num to 1.
            else Sym=1; //if password contain symbols then set Sym to 1
        }
        if(Lower==1 && Upper==1 && Num==1 && Sym==1) Strength="Strong"; // if password contain all cases then password is strong.
        if((Lower==0 && Upper==1 && Num==1 && Sym==1)|| (Lower==1 && Upper==0 && Num==1 && Sym==1) || (Lower==1 && Upper==1 && Num==0 && Sym==1) || (Lower==1 && Upper==1 && Num==1 && Sym==0))
            Strength="Good"; //if password lack any one case only then password's strength is Good.
        if ((Lower==0 && Upper==0 && Num==1 && Sym==1) || (Lower==0 && Upper==1 && Num==0 && Sym==1) || (Lower==1 && Upper==0 && Num==0 && Sym==1) || (Lower==1 && Upper==1 && Num==0 && Sym==0) || (Lower==0 && Upper==1 && Num==1 && Sym==0) || (Lower==1 && Upper==0 && Num==1 && Sym==0))
            Strength="Weak"; // if password contain only two cases only then password is weak.
        if((Lower==0 && Upper==0 && Num==1 && Sym==0) || (Lower==0 && Upper==1 && Num==0 && Sym==0) || (Lower==1 && Upper==0 && Num==0 && Sym==0) ||(Lower==0 && Upper==0 && Num==0 && Sym==1))
            Strength="Very Weak"; // if password contain only one character set then password is very weak.
    }
    cout<<"\nStrength of your password is: "<<Strength<<endl; //printing strength of password.
}

int main()
{
    int cho;
    while (1) {
        system("cls");
        cout<<"What you want me to do?\n1. Generate Password \n2. Test Strength of Password \n1/2> "; cin>>cho;
        if (cho==1)
        {
            int length,numbers;
            char ch[4]; //0= lower 1=upper 2=num 3=special
            cout<<"Length of password: "; cin>>length;       //asking for length of passwords.
            cout<<"Number of passwords: "; cin>>numbers;     //asking for number of passwords to generate.
            cout<<"\nSelect characters to generate passwords:-\n";
            cout<<"Lower case alpha: [y/n]: "; cin>>ch[0];     //asking to use lower case alpha
            cout<<"Upper case alpha: [y/n]: "; cin>>ch[1];     //asking to use upper case alpha
            cout<<"Numeric characters: [y/n]: "; cin>>ch[2];   //asking to use numbers
            cout<<"Special characters: [y/n]: "; cin>>ch[3];   //asking to use symbols
            for(int i=0; i<4; i++)    //checking all input given is appropriate or not.
            {
                if(ch[i]=='y' || ch[i]=='Y' || ch[i]=='n' || ch[i]=='N')
                    continue;
                else {
                    cout<<endl<<"Invalid input............"<<endl;
                    system("pause");
                    system("cls");
                    exit(0);
                }
            }
            PGenerator(length,numbers,ch); //calling PGenerator function to generate passwords.
        }
        else if (cho==2)
            PTester(); //calling PTester function to test strength of function.
        else exit(0);
        system("pause > nul");
    }
}



HOPE YOU WILL ENJOY!  😊


No comments

Powered by Blogger.