×

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required.

Password: A password is a word that permits access to somewhere or something. A password is for the safety of your private information. Passwords usually contain digits, alphabets, and special characters. A strong password includes alphabets, numerals, at least one capital letter, and special characters.

Alphabets - {a to z, A to Z} 
Digits - {0 to 9}
Special characters such as @, $, #, & etc.

A password should not be your name, mobile number, or a sequence of numbers or characters such as {123456}. These are considered weak passwords, and they can be hacked easily. The below program is for a password generator.

Program

#include <iostream>
#include <cstdlib>   //contains rand(), srand() functions
#include <ctime>
using namespace std; 
static const char alphanumeric[]="0123456789" "!@#$%^&*" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"; 
int stringLen=sizeof(alphanumeric)-1; 
char RandomGen()
{ 
return alphanumeric[rand()%stringLen]; 
} 
int main() 
{ 
int n,c=0,s=0;
srand(time(0));
cout<<"Enter length of the password:";
cin>>n;
cout<<n<<endl;
cout<<"Password generated is:";
N:
char C;
string D;
for(int z=0; z < n; z++) 
{ 
C=RandomGen();
D+=C;
if(isdigit(C))
{
c++;
}
if(C=='!' || C=='@' || C=='$' || C=='%' ||  C=='^' || C=='&'|| C=='*'|| C=='#')
{
s++;
}
}
if(n>2 && (s==0 || c==0))
{
goto N; 
}
cout<<D; 
return 0; 
}

//A password usually has at least eight characters.

Output 1

Enter length of the password:8
8
Password generated is:3wF3$jD4

In Output 1, the length of the password is eight characters long. Therefore, a random 8-character password has been created. The password generated is “3wF3$jD4”. The generated password contains digits, special characters, and alphabets. Consequently, it is considered a strong password.

Output 2

 Enter length of the password:9
9
Password generated is:RTTC*RrTC

In Output 2, the length of the password is taken as 9. Therefore, a random 9-character password is created. The password generated is “RTTC*RrTC". The generated password contains digits, special characters, and alphabets. Consequently, it is considered a strong password.

Output 3

Enter length of the password:15
15
Password generated is:$6h*pSDxJ4&J7A^

In Output 3, the length of the password is taken as 15. Therefore, a random 15-character password is created. The password generated is “$6h*pSDxJ4&J7A^”. The generated password contains digits, special characters, and alphabets. Therefore, it is considered a strong password. Here, the RandomGen() function returns

Explanation of the program

For generating a password, a randomizing function is used. The cstdlib library in C++ has this randomizing function. Alphanumeric characters include alphabets and numbers.

rand() is the inbuilt function in C++ language, this function is available in <cstdlib>. A randomized sequence is generated by using rand(). Every time the code is executed, a new password is generated. The srand() function is an inbuilt function in C++ is available <cstdlib>. The srand() function

static const char alphanumeric[]="0123456789" "!@#$%^&*" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz";

The alphanumeric[] contains the numbers, special characters, and alphabets (lower and upper). By using this program, one can use the generated password of any specified length.

C++ Program to check if the password is correct or incorrect

#include <iostream>
#include <string>
using namespace std;


int main(){
    string password = "pass1234";
    string input;
    cout << "enter the password: ";
    cin >> input;
    if (input==password){
        cout << "Correct password" << endl;
    }else{
        cout << "Incorrect password" << endl;
    }
    return 0;
}

Output 1

enter the password: pass1234
Correct password

In Output 1, the user enters the string as pass1234. The string entered by the user matches the password. Therefore, the user input is the correct password.

Output 2

enter the password: hello1234
Incorrect password

In Output 2, the user enters the string as hello1234. The string entered by the user does not match the password. Therefore, the user input is the Incorrect password.

Output 3:

enter the password: pass123
Incorrect password

In Output 2, the user enters the string as pass123. The string entered by the user does not match the password. Therefore, the user input is the Incorrect password.

Explanation of the program

In the above program, the string variable password contains the sequence "pass1234". The user enters a string sequence, and the entered sequence compares with the stored sequence ("pass1234"). The password is correct when "pass1234" matches with the sequence entered by the user, and the password is incorrect when "pass1234" does not correspond with the sequence entered by the user.


Related Topics

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

Returning Multiple Values from a Function using Tuple and Pair in C++

We may come across many situations where after the driver code's execution is performed in a code block, the return should be either multiple values or a single value possibly...

4 minutes read.

swap() function in C++

Swap() function: swap() function in C++: swap() function is a pre-define function in c++ present in STL( Standard template library ). It is used to swap two numbers. It takes two mandatory...

6 minutes read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

C++ Bitset

Overview In C++, bitset represents a fixed-sequence of some bits values by either 0 and 1. Zero represents the value as false or unset, while 1 represents the value as true...

4 minutes read.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

6 minutes read.

Processing strings using std string stream in C++

A string class object called std: string stream is used to streamline into various variables, just as files can pour into strings. This class's things make use of a string...

3 minutes read.

How to improve programming skills in C++

Before getting started, one should know why to improve their programming skills. To become a good software developer or programmer, one must be skilled in at least one programming language. Many...

4 minutes read.

Dynamic Constructor in C++

Dynamic constructors create dynamic memory by using a dynamic memory allocator new within the constructor. This allows us to initialize objects dynamically. The term dynamic constructor is used when memory...

2 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

Binary Search in C++

The binary search in the C++ programming language will be discussed. By continually halves the array and then seeking specified items from a half array; binary search is a technique...

8 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

Difference between C and C++

What do you mean by C? C is a machine-independent structure or procedural oriented computer language that is widely utilized in a variety of applications. C is a fundamental programming language...

4 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.