Building a Password Generator in C - C Projects

Overview
In the internet era, to use most websites or apps, users need to create an account, and for the users' privacy, they must log in with a username and password. Passwords are the main key to stopping the security breach that’s why passwords must be unique and strong. We are going to create a script in the C programming language which will generate a random password of the required length using the different sets of characters and this script will surely provide a unique password for each call.
What are We Building?
In this project, we will build a script in C programming language that will take a single integer as an input and provide a string of random characters as the output. Our goal is to get the output completely random and unique each time. If the password we are getting is not unique then there are high chances that the password of the users can be detected by the others. Also, we need to create the script in a way that there are a large number of outputs it can generate because if there are fewer passwords then by just brute force the passwords of the users can be generated and it will not work efficiently.
Pre-requisites
To build this project we need to go through some topics to get some prior knowledge of them for that we will discuss them in brief and there will be some references of articles which are beneficial if you are not experienced in them:
- First of all the final output will be the string or character array which will be consists of the characters such as lowercase and uppercase English letters, number, and special characters. So, we need to learn about the strings and variables in the C programming language.
- To learn about the variables declaration please refer to the article Declaration of Variables in C.
- To learn about the arrays in C please refer to the article Array Data Structure.
- To create the final password of a given length we need to traverse over the loop which may be a for loop or while loop.
- To learn about the for loop please refer to the article For loop in C.
- We have different types of characters and so add them to the character array we need some cases and to execute that we need to learn about if-else statements..
- To learn about the if-else statements please refer to the article if-else Statement in C.
- As we are going to take elements randomly we are going to use the function srand() and rand() function of C. Also, we are going to use the time() function from the time.h library which we will discuss later in this article.
- To learn about the srand() and rand() functions please refer to the article Random Number Generator in C.
- To learn about the time function please refer to the article Library Functions in C.
How are We Going to Build the Survey Form?
The steps to build this project are not much complicated and are very straight. Let’s directly move toward them:
- Initially, we are going to take user input which will define the length of the password the user needs.
- We will define four character arrays in which the first two will be of length 26 and contains the uppercase and lowercase English alphabets, the third array will be of length 10 and contains all the numeric digits, and the fourth array will contain the special characters like &,$,@,%, etc.
- time() function returns a number, that is the number of seconds passed since 00:00:00 UTC, January 1, 1970, which means function time() will return a different number at each call. We will pass the return value of the time() function as an argument to the srand() function to set the seeds of the random number generator algorithm differently each time.
- We will run a for loop to traverse over the character array in which we want to store our final output.
- At each iteration, we will call the rand() function to get a random number and take its modulo with 4 to get a value in the range 0 to 3 (both inclusive).
- On the basis of the last value we obtained, we will execute the if-else statements which will eventually give us four different cases.
- In each case, we will get a different array from the above-discussed four. For each of them, we will call another random value and take its modulo with the length of the current value to get a random index to add to the current index of the character array.
- At last, we will give the output which is stored in the character array.
Final Output
The final output from our code will be just a string or set of characters and the number of characters will equal the length provided by the user. As we are going to use the random number generator algorithm, the output will be always unique. Let’s see some possible final outputs:
Requirements
To create this project we need some functions such as srand(), time(), etc, and to use them we need to include some libraries (in which these functions are pre-defined) in our code. Let’s discuss these libraries:
- stdio.h: stdio.h is the standard library that stands for the standard input and output. In this library, basic input and output functions are defined that is scanf() and printf() which are used to take user input and provide the final general output to the user.
- stdlib.h: stdlib.h is the header file in which the srand() and rand() functions are already defined and to use srand() and rand() functions we need to include this header file in our script or C program.
- time.h: time.h header file contains the function time() which will return a number that will indicate the number of seconds passed since 00:00:00 UTC, January 1, 1970 (Unix timestamp).
Building the Password Generator in C
Now the main part of this article will begin, that is writing the script. We will break the project into many parts to make steps to create project easy to understand and will implement the above-provided steps.
- Adding libraries in the code
In this step, we will add the above-discussed libraries into our code.
Here we have added all the three above-defined libraries.
- Starting the main function and defining character array's:
In this section, we will start the main function and define the character arrays.
We have defined the character arrays which are array 'numbers' containing the digits, array letter containing lowercase English alphabets, array LETTER containing uppercase English alphabets, and at last character array symbols which will contain the symbols.
- Defining variables for input/output functions:
In this section, we will define variables to take the user input and store the output that is the required random password.
Here, we have declared an integer variable size and stored the user input in it. After that, we created a character array password of size 20 (which is its maximum capacity).
- Calling the srand function
In this section, we will call the srand() function to provide the seeds to the random number generating algorithm.
Here, we have called srand() function and passed time() function as its argument. The return value of the time() function will not be stored anywhere as we have passed NULL as its parameter and we will typecast the return value to unsigned int.
- Traversing over the array:
In this section, we will perform the main functioning of the project which is the generation of random characters by using the rand function, for loop, if-else conditions, and pre-defined character arrays.
In the above part of the code, we have declared a for loop which will run a size number of times. At each iteration of the loop, we will call the rand() function and take mode with 4 of its return value and store it in the variable random_number. According to the value of the random_number if-else statements are executed. In each if-else statement, we again called to rand() function and took return value mode with the length of the current character array, and stored again the variable random_number. At last, for the ith index of the password array, we added the value of the current character’s random_number index value.
- Providing the output and ending the main function:
In the last, we just printed the final answer or password.
We just traversed over the password array using for loop up to its length and at each iteration printed the value of the current character.
Note:
- The time complexity of the above code is O(N), where N is the size of the password the user needs because we have traversed for loop N times, and for printing the output we again traversed over the array of size N. Also, operations like calling the rand() function and taking modulo, takes constant time which makes the final time complexity of the program O(N).
- The space complexity of the above code is O(N + 70) or O(N) because we have used only four character arrays of constant size 26 for lowercase English alphabets, 26 for uppercase English alphabets, 10 for digits, and 8 for special characters. To store the output we have created an array of size 20 which may vary as per the requirements of the user, so we can take its size as N.
What’s Next
This project is a beginner-level project and there are many more improvements or upgrades that are possible to make it more complex and challenging. Let’s discuss some of them:
- Instead of taking a single number input as size, we can take two numbers which will represent the range of length of the password, it will make this project a random password generator with random length.
- The above-created script provides the password as result but there is no boundation on the repetition of any character or digit. We can put some restrictions on the use of any character like no same character will be used more than x number of times or no two places can be occupied by the same character.
- Users can put boundation over the minimum one uppercase English alphabet must be present. Similarly, to lowercase English alphabets, number digits, and a special symbol or the special characters.
Conclusion
- We have built a script in C programming language that will take a single integer as an input and provide a string of random characters as the output.
- The script is created in a way that there are a large number of outputs it can generate because if there are fewer passwords then using brute force passwords can be generated.
- We have created the script by using a different set of characters like lowercase English alphabets, uppercase English alphabets, digits, and some special characters.
- The srand() function is used to provide the different seeds to the random number generating algorithm so it will generate a different set of numbers at each call.
- We passed the return value of the time() function (that is the number of seconds passed since 1 January 1970) to pass a unique value to the srand() function at each call.
- There are many upgradations can be done to this project like making the length of the password random, setting the frequency of the set of characters limited, etc.