Building a Password Generator in C++
Overview
In this world of digitalization, security is a very important issue. Everything that is digital-based must need a password to protect it. There is much need for a password to encrypt or lock stuff so that everything should not be accessible to everyone. Only the authorized person who has the password can access that particular stuff. Having a strong password ensures authenticity and security. In this article, we will learn how to make a random password generator in c++ language.
What are We Building?
The random password generator in c++ language is a program that will generate strong random passwords of the specified length using alphabets, numbers, and symbols. Here we will do a program in c++ language that will consist of a menu-driven program that will generate a random password in c++. Let us first learn the prerequisites and build the intuition of the program so that the implementation can become easier.
Pre-requisites
A good understanding of c++ language is a must pre-requisite for understanding this project in a better and more efficient manner. There are various concepts that are involved in the project "random password generator in c++". These concepts are as follows:
- Variables in C++
- Data types in C ++
- Array in C++
- Loop in C++
- Rand() function in C++
- Switch case in C
Following are the steps that are included in the project password generator in c++ to solve the problem:
- First we will take the length of the password and then a character array will be declared. This character array will be used to store the password.
- After that we will declare a character array for each type of alphabet like small letters, capital letters, numbers, and special characters.
- Then we will apply the conditional statements that will get executed in the program and after the execution of the program, a random password will be generated.
Let us now learn the overall idea and the steps needed to develop a random password generator in Python.
How Are We Going to Build the Survey Form?
The idea of building a random password generator in Python is very simple. We will first create a string or list consisting of all the alphabets (both small alphabets and capital alphabets), numbers, and symbols.
Now, we will take the length of the required password as input from the user. Finally, we will use a loop ranging between 0 and the length of the password, and in every iteration, we will randomly choose a letter from the set of letters defined above and store them into a resultant password.
Refer to the following sections for more clarity and implementation.
Final Output
This is how the final output of the project "number generator in c++"
Requirements
Following are the things that we need to make the project password generator in c++.
- Header Files:
- C++ <cstdlib>: This header file consists of some set of general-purpose functions that is used to convert a string value to a double value. This header file also contains some mathematical functions. Some examples are abs(), which are used to find the absolute value of a number.
- C++ <Iostream>: It stands for standard input-output. This header file consists of some object definitions such as cin, cout, cerr, etc..
- C++ <ctime>: This header file is used for converting the given time since epoch to a local time of calendar. It also then represents this in a character form.
Check out this article to learn more about, Header Files in C++.
-
There are also some functions that are used in this program that are as follows:
-
array: An array in c++ is defined as a group that contains an element of similar types. In c++, the indexing of an array starts from 0, and in it, we can set some fixed elements.
-
loop: In general, a loop in c++ is used to repeat a particular set of lines of code many times such as For loop
-
rand() function: rand() function in c++ is used to generate random numbers for a given range in our code. For a computer system, it is not possible to generate any random number because it is a machine and it works on any task on the basis of 0 and 1 so, it can not sense randomness. Hence, a thing called PRNG (Pseudo Random Number Generator) that generates a sequence of random numbers is used in c++.
-
Building the Password Generator in C++
Now, let us see the approach step by step that we have taken to build this project "password generator in c++".
1. Defining the selectArray() Function.
Explanation:
We have first declared a function namely - selectArray() that takes no parameters. The function returns a random number between 1 and 4 using the pre-defined function srand() and rand().
2. Defining the getKey() Function.
Explanation:
Now, getKey() is a function that is somewhat similar to the selectArray() function but instead of generating a number between 1 and 4, it generates a random number between 0 to 25.
3. Defining the generate_password() Function.
Explanation:
The generate_password() function takes one argument which resembles the length of the required password. In the function, we have declared an empty string that will store the generated password. After that we initialized four strings, the first string (alphabet) stores all the alphabets in CAPITAL letters, the second string (alphabet) stores all the small letter alphabets, the third string (s_symbol) stores the various symbols, and the fourth string (number) stores the numbers. now, we would initialize four variables that will initially point to the starting index of these four previously defined strings.
After these declarations, we will initialize a count variable which will keep on incrementing in each iteration. It will help us to detect if we have generated a password of the specified length or not. If the count variable's value reaches length (provided as an argument to the current function) then we would exit the loop. Now inside the loop, we would select any one of the four declared strings using the selectArray() function. We have to select only alphabets in the first place so we must check if the selectArray() function has selected either the first string or second string.
4. Defining the Various Cases
Explanation:
We are using a switch case for handling various cases. The first case will take care of whether the specified minimum requirement of alphabet characters (small letters) has been fulfilled or not. So, it will first find a character using the getKey() function then it will append that character at the end of the currently generated password. After adding the character, it will increment the counter and break the switch case.
Similarly, the second case takes care if the specified minimum requirement of alphabet characters (CAPITAL letters) has been fulfilled or not. It will perform the same function thereafter.
The third case takes care of the numbers and the fourth case is taking care of the symbols.
All of these functions are working in a similar manner. They are first picking up one character and then appending it at the end of the password string and incrementing the counter.
5. Defining the Main Function
Explanation:
Finally, we have the main() function that uses a do-while loop and switch case for taking input from the user and then calling the adequate function. In the main function, we are asking the user to select from the option. If the user selects option 2 then we will call the exit() function else we will go to the respective case and perform the specified instructions.
The first case asks the user about the password length and checks whether the user has entered a number greater than 7 or not because the password must be greater than 7. Similarly, there is a check for the maximum length of the password as the maximum range is 100 letters. If both of the corner cases are not there then we can generate the password by calling the generate_password() function.
In the default section, there are a couple of print statements that will tell the user that he/she has selected the wrong option and also provides an option for selecting a new choice.
What’s Next
This project is a very basic level project. You can also improve this project by adding some databases and a management system. First, you can go through this project to understand all the basic concepts and then try to implement concepts like objects and classes in the project to make a successor of this basic project.
Conclusion
- password generator in c++ is a kind of console-based software that can be used to generate random passwords.
- These passwords can be composed of numbers, alphabets, special characters, etc to produce a random password.
- Different concepts that we have used in this project are variables, data types, arrays, loops, rand() functions, and switch cases.
- Various header files that are included in this project are <cstdlib>, <iostream>, and <ctime>.