Creating a Wi-fi password generator using C++
Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create a simple Wifi Password Project using C++.
This is a simple C++ program that utilizes the Windows command prompt to obtain the user’s computer’s stored Wi-fi passwords and save them to a text file. It is a simple App to retrieve passwords of your known Wi-fi networks that you have connected to before on your device. The retrieved passwords will be stored in a ‘.txt’ file. You can open the file to take a look at all the retrieved passwords.
Before we dive into our project source code, let’s understand what exactly a wifi password generator is and what its purpose is.
What is a Wi-Fi password generator?
A WiFi password generator is a simple program that uses the Windows command prompt. It provides a command-line interface (CLI) to the user, which helps in retrieving all the saved wifi passwords stored on the user’s computer and saving them in a text file for future purposes.
What is the use of a Wi-Fi password generator?
The main purpose of a wi-fi password generator is to store all the passwords of wifi that are connected to the user’s computer and store them in a separate file that can be used later by the user if he or she forgets any of the passwords.
Prison Management System using C++ (With Source Code)
Creating a Wi-fi password generator using C++Source code:
You can directly use this code by copying it in your IDE to understand the working and then can create it by understanding the project.
#include <iostream> #include <sstream> #include <fstream> #include <string.h> #include <stdexcept> #include <stdio.h> #include <string> #include <vector> using namespace std; string exec(const char* cmd) { char buffer[128]; string result = ""; FILE* pipe = _popen(cmd, "r"); if (!pipe) throw runtime_error("_popen() failed!"); try { while (fgets(buffer, sizeof buffer, pipe) != NULL) { result += buffer; } } catch (...) { _pclose(pipe); throw; } _pclose(pipe); return result; } vector<string> getWifiList() { stringstream ss(exec("netsh wlan show profile").data()); string input; vector<string> wifi; while(getline(ss,input)) if (input.find("All User Profile") != string::npos) wifi.push_back(input.substr(27,input.length())); return wifi; } string getPassword(string ssid) { string command = "netsh wlan show profile \"" + ssid + "\" key=clear"; stringstream ss(exec(command.data()).data()); string input; while(getline(ss,input)){ if (input.find("Key Content") != string::npos) return input.substr(29,input.length()); } return "< NULL >"; } int main() { cout << "Getting list of known wifi networks..\n\n"; vector<string> wifi = getWifiList(); ofstream ofs("saved-wifi-passwords.txt"); for (string ssid: wifi) { cout << "Getting password for " << ssid << "..\n"; ofs << "SSID\t:\t" << ssid << "\n"; ofs << "Key\t:\t" << getPassword(ssid) << "\n\n"; } ofs.close(); cout << "\nOuput saved to `saved-wifi-passwords.txt`..\n"; return 0; }
Now let us understand the code:-
- We will start off by writing the header of the code with the required libraries – <iostream>, <sstream>, <fstream>, <string.h>, <stdexcept>, <stdio.h>, <string>, <vector>.
- Now we will define a function called exec that takes a command as a parameter and returns the output of that command. It uses error handling – try-catch block, if statement and a while loop. This function is used to retrieve the Wi-Fi information.
- We will create a vector or string type called getWifiList(). It uses the function called exec to execute the “netsh wlan show profile” command and then retrieve a list of all the saved Wi-Fi networks for your device. Then this function extracts the SSID of each network and adds it to a vector of string type called wifi.
- Creating a function called getPassword of string type which takes SSID as a parameter constructs a ‘netsh’ command to retrieve the password for that network. It uses the ‘exec’ function to execute the command and retrieve the password from the output.
- The main() function firstly prints “Getting list of known wifi networks..”, on the console. It retrieves the list of saved Wi-Fi networks using ‘getWifiList’. It then iterates over each network, retrieves its password using ‘getPassword’, and saves the SSID and password to a text file called ‘saved-wifi-passwords.txt’.
- At last the program prints a message on the console stating that the output has been saved to the .txt file. We can check the password list in that file – “Ouput saved to `saved-wifi-passwords.txt`..”.
- This sums up our Simple Wi-fi Password Project using C++.
Simple College Management System using C++ (With Source Code)
Final Output:-
Below is the final output of the Wi-fi password generator using C++. The above password generated gonna boost your resume and you can add the project in C++ language.
Here is an example to show how this project works.
After running the C++ program on our IDE:
The passwords are saved in the saved-wifi-passwords.txt file.
Conclusion
We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting. This project you can also use in a College minor project and this article will be helpful for college students.
Electricity Bill generator using C++ (With Source Code)
If you enjoyed the article and learned something new today, let us know in the comments.
Thank you.
Happy Coding! 🙂
ADVERTISEMENT
Follow: CodeWithRandom
ADVERTISEMENT
What is a Wi-Fi password generator?
A WiFi password generator is a simple program that uses the Windows command prompt. It provides a command-line interface (CLI) to the user, which helps in retrieving all the saved wifi passwords stored on the user’s computer and saving them in a text file for future purposes.
ADVERTISEMENT
What is the use of Wi-fi password generator?
The main purpose of password generator is to store all the passwords of wifi’s that are connected to the user computer and store them in a separate file which can be used later by the user if he/she forgots any of the password.
ADVERTISEMENT