×

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

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

4 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

Differences Between C Structures and C++ Structures

The below table clearly describes the differences between C and C++ programming languages Structures concepts where the core principle concepts like static members, data handling, pointers, access modifiers, the importance...

3 minutes read.

C++ array of Pointers

Array of Pointers: In high-level programming languages like C++, the array's name is its pointer. The name of an array contains an address which is the address of an element. In...

4 minutes read.

Pthreads or POSIX Threads in C++

The thread API for C/C++ is implemented by pthreads or POSIX threads. It enables the multithreading system, which enables parallel and distributed processing, and the creation of new concurrent process...

3 minutes read.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

Learn C++ Tutorial

C++ Introduction C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories. It is superset (extension) of C programming language. Depending upon features supported by programming...

10 minutes read.

Structure Vs Class in C++

The structure in C++ is similar to that of a class, with a few exceptions. Both the structure and the stage, the most important thing is safety. The property is...

4 minutes read.

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.

Remove duplicates from sorted array in C++

Remove duplicates from the sorted array in C++. This same process, due to a sorted array, is to erase the redundant components from the array. Examples: Input  : arr[] = {2, 2, 2,...

4 minutes read.

Top 5 IDEs for C++ That You Should Try Once

In the past decades, creating an application or interface from the very basic idea, the developer has to struggle a lot for it. Because an application is a combination of...

3 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

3 minutes read.

Sliding Window Technique in C++

Sliding Window Technique or Window Sliding Technique is a computational technique that is mainly used to reduce the use of nested loop and replace it with a single loop. It...

4 minutes read.

gmtime() function in C/C++

C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which...

4 minutes read.