Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <stdlib.h>
- #include <time.h>
- #include <vector>
- #include <unordered_map>
- #include <iostream>
- #include <string.h>
- using namespace std;
- string Generate_Password(int char_number, bool add_special_char, bool add_upper_letters, bool add_lower_letters, bool add_number);
- int main()
- {
- srand( time( NULL ) );
- cout << "__________________________________________________________\n" << endl;
- cout << " PASSWORD GENERATOR PROGRAM\n" << endl;
- cout << "__________________________________________________________\n" << endl;
- bool mainloop_error(false);
- do {
- cout << "\n" << endl;
- cout << "--> Generate Random Password : Press G...\n" << endl;
- cout << "--> Show Password List : Press L...\n" << endl;
- string encrypted_password;
- char user_input_sec_menu;
- char user_input_menu;
- cin >> user_input_menu;
- cout << "\n" << endl;
- if (user_input_menu == 'G')
- {
- int user_char_number;
- bool char_number_loop_error;
- do {
- cout << "Choose the number of character :\n" << endl;
- char_number_loop_error = false;
- user_char_number = 0;
- cin >> user_char_number;
- if (cin.fail())
- {cout << "Wrong command !!\n" << endl;
- char_number_loop_error = true;
- cin.clear();
- cin.ignore(256,'\n');
- }
- else {char_number_loop_error = false;}
- }while(char_number_loop_error == true);
- cout << "\n" << endl;
- bool include_numbers;
- bool include_numbers_loop_error;
- do {
- cout << "Do you want your password to include numbers ? (y/n)\n" << endl;
- include_numbers_loop_error = false;
- char password_include_numbers;
- cin >> password_include_numbers;
- cout << "\n" << endl;
- if (password_include_numbers == 'y')
- {include_numbers = true;}
- else if (password_include_numbers == 'n')
- {include_numbers = false;}
- else {cout << "Wrong command !!\n" << endl;
- include_numbers_loop_error = true;}
- }while(include_numbers_loop_error == true);
- cout << "\n" << endl;
- bool include_upper_letters;
- bool include_upper_letters_loop_error;
- do {
- cout << "Do you want your password to include uppercase letters ? (y/n)\n" << endl;
- include_upper_letters_loop_error = false;
- char password_include_upper_letters;
- cin >> password_include_upper_letters;
- cout << "\n" << endl;
- if (password_include_upper_letters == 'y')
- {include_upper_letters = true;}
- else if (password_include_upper_letters == 'n')
- {include_upper_letters = false;}
- else {cout << "Wrong command !!\n" << endl;
- include_upper_letters_loop_error = true;}
- }while(include_upper_letters_loop_error == true);
- cout << "\n" << endl;
- bool include_lower_letters;
- bool include_lower_letters_loop_error;
- do {
- cout << "Do you want your password to include lowercase letters ? (y/n)\n" << endl;
- include_lower_letters_loop_error = false;
- char password_include_lower_letters;
- cin >> password_include_lower_letters;
- cout << "\n" << endl;
- if (password_include_lower_letters == 'y')
- {include_lower_letters = true;}
- else if (password_include_lower_letters == 'n')
- {include_lower_letters = false;}
- else {cout << "Wrong command !!\n" << endl;
- include_lower_letters_loop_error = true;}
- }while(include_lower_letters_loop_error == true);
- cout << "\n" << endl;
- bool include_special_char;
- bool include_special_char_loop_error;
- do {
- include_special_char_loop_error = false;
- cout << "Do you want your password to include special characters ? (y/n)\n" << endl;
- char password_include_special_char;
- cin >> password_include_special_char;
- cout << "\n" << endl;
- if (password_include_special_char == 'y')
- {include_special_char = true;}
- else if (password_include_special_char == 'n')
- {include_special_char = false;}
- else {cout << "Wrong command !!\n" << endl;
- include_special_char_loop_error = true;}
- }while(include_special_char_loop_error == true);
- cout << "\n" << endl;
- string password;
- password = Generate_Password(user_char_number, include_special_char, include_upper_letters, include_lower_letters, include_numbers);
- cout << "Your password is : " <<password << endl;
- cout << "\n" << endl;
- bool continue_generate_new_password;
- do {
- continue_generate_new_password = false;
- cout << "--> Generate a new password with the same settings : Press N...\n" << endl;
- cout << "--> Save your password : Press S...\n" << endl;
- cout << "--> Go back to Main menu : Press M...\n" << endl;
- cin >> user_input_sec_menu;
- cout << "\n" << endl;
- if (user_input_sec_menu == 'N')
- {
- password = Generate_Password(user_char_number, include_special_char, include_upper_letters, include_lower_letters, include_numbers);
- cout << "Your password is : " << password << endl;
- cout << "\n" << endl;
- continue_generate_new_password = true;
- }
- if (user_input_sec_menu == 'S')
- {
- cout << "Give it a name (to be recognised when listed) :\n" << endl;
- string password_name;
- cin >> password_name;
- cout << "\n" << endl;
- ofstream save_password("Passwords.txt", ios::app);
- if(save_password) //On teste si tout est OK
- {
- save_password << password_name << " : " << password << "\n" << endl;
- cout << "Password saved !\n" << endl;
- }
- else
- {
- cout << "ERREUR: Impossible d'ouvrir le fichier." << endl;
- }
- mainloop_error = true;
- }
- }while(continue_generate_new_password == true);
- if (user_input_sec_menu == 'M')
- {
- mainloop_error = true;
- }
- }
- else if (user_input_menu == 'L')
- {
- ifstream password_list_stream("Passwords.txt");
- string password_list;
- while (getline(password_list_stream, password_list)) {
- std::cout << password_list << "\n";
- }
- mainloop_error = true;
- }
- else {cout << "Wrong command !!\n" << endl;
- mainloop_error = true;}
- }while (mainloop_error == true);
- system("pause");
- return 0;
- }
- string Generate_Password(int char_number, bool add_special_char, bool add_upper_letters, bool add_lower_letters, bool add_number)
- {
- string user_password;
- char random_special_char;
- char random_numbers_char;
- char random_upper_letters_char;
- char random_lower_letters_char;
- char password_char;
- int password_size(0);
- ifstream char_list("PasswordChar.txt");
- for (int i = 0; i < char_number; i++)
- {
- vector<char> special_char_vector;
- vector<char> numbers_char_vector;
- vector<char> upper_letters_char_vector;
- vector<char> lower_letters_char_vector;
- special_char_vector.clear();
- numbers_char_vector.clear();
- upper_letters_char_vector.clear();
- lower_letters_char_vector.clear();
- if (add_special_char == true)
- {
- char_list.seekg(0, ios::beg);
- vector<char> special_char_vector; //32 cases
- int e(0);
- while(e < 29)
- {
- char_list.get(password_char);
- special_char_vector.push_back(password_char);
- //cout << password_char << endl;
- e += 1;
- }
- int random_special_char_index = rand() % 29;
- random_special_char = special_char_vector[random_special_char_index];
- //cout << random_special_char << endl;
- }
- else {random_special_char = ' ';}
- if (add_number == true)
- {
- char_list.seekg(55, ios::beg);
- vector<char> numbers_char_vector; //32 cases
- int e(0);
- while(e < 10)
- {
- char_list.get(password_char);
- numbers_char_vector.push_back(password_char);
- //cout << password_char << endl;
- e += 1;
- }
- int random_numbers_char_index = rand() % 10;
- random_numbers_char = numbers_char_vector[random_numbers_char_index];
- //cout << random_numbers_char << endl;
- }
- else {random_numbers_char = ' ';}
- if (add_upper_letters == true)
- {
- char_list.seekg(29, ios::beg);
- vector<char> upper_letters_char_vector; //33 cases
- int e(0);
- while(e < 26)
- {
- char_list.get(password_char);
- upper_letters_char_vector.push_back(password_char);
- //cout << password_char << endl;
- e += 1;
- }
- int random_upper_letters_char_index = rand() % 26;
- random_upper_letters_char = upper_letters_char_vector[random_upper_letters_char_index];
- //cout << random_letters_char << endl;
- }
- else {random_upper_letters_char = ' ';}
- if (add_lower_letters == true)
- {
- char_list.seekg(65, ios::beg);
- vector<char> upper_letters_char_vector; //33 cases
- int e(0);
- while(e < 26)
- {
- char_list.get(password_char);
- lower_letters_char_vector.push_back(password_char);
- //cout << password_char << endl;
- e += 1;
- }
- int random_lower_letters_char_index = rand() % 26;
- random_lower_letters_char = lower_letters_char_vector[random_lower_letters_char_index];
- //cout << random_letters_char << endl;
- }
- else {random_lower_letters_char = ' ';}
- char possibilities_array[4] = {random_special_char, random_numbers_char, random_upper_letters_char, random_lower_letters_char};
- int possibilities_array_index = rand() % 4;
- user_password += possibilities_array[possibilities_array_index];
- while (possibilities_array[possibilities_array_index] == ' ')
- {
- password_size = 0;
- password_size = user_password.size();
- user_password.erase(password_size-1, 1);
- possibilities_array_index = rand() % 4;
- user_password += possibilities_array[possibilities_array_index];
- }
- }
- return user_password;
- }
Add Comment
Please, Sign In to add comment