Creating a Password generator application using C++

Password generator using C++

Well, this is my very first application that I've done in C++. The ideas of this application sprouted when I was answering this question (click to see) in one of the Stack exchange sites called code review. This post consists of the source code for a password-generating application. Here are some of the features of the application:


  1. You can either generate a classic or custom password
    •  A Classic password is a password generated from predefined characters A-Z, a-z, 0-9 and the symbols  (space) ! # $ % & ' ( ) * + , - . / \ " : ; < = > ? @ [ ] ^ _ ` { | } ~. These passwords are not accepted by all websites but only a few.
    • A Custom password is a password generated from the characters that you input! Just mention all the characters accepted by the website and you'll have your password generated.
  2. You can Bookmark the generated passwords if you like them and look at them anytime you want!
This application is not in anyway ingenious. This is just done out of my will to create something using the object-oriented programming techniques. Any critical comment is much appreciated :)

Source code for Password generator:

 #include <stdio.h>  //use stdafx.h for microsoft visual studio
 #include <stdlib.h>  
 #include <string.h>  
 #include <time.h>  
   
 void clear()  
 {  
      system("cls");  
 }  
   
   
 class Mechanism  
 {  
 private:  
      char characters[2][129] = {"abcdefghojklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !#$%&'()*+,-./\\\":;<=>?@[]^_`{|}~","custom character set!"};  
      char password[100];  
 public:  
      //password generating mechanism  
      char* password_generator(int type, int length)  
      {  
           if (type == 1)  
           {  
                char custom_characters_set[129];  
   
                printf("\n\nenter your custom character set (press 'enter' key to end the entry) : \n");  
   
                scanf_s("%s", custom_characters_set, 128);  
                strcpy_s(characters[1], custom_characters_set);  
           }  
   
           for (int index = 0; index < length; index++)  
           {  
                password[index] = characters[type][rand() % strlen(characters[type])];  
           }  
   
           password[length] = '\0';  
   
           return password;  
      }  
   
      //password bookmarking mechanism  
      void bookmark_password(char* password)  
      {  
           FILE *file_pointer;  
           char ch;  
   
           fopen_s(&file_pointer, "password_bookmarks.txt", "a");  
   
           clear();  
   
           if (file_pointer == NULL)  
           {  
                printf("file creation error. press any key to exit application : ");  
                scanf_s(" %c", &ch, 1);  
                exit (-1);  
           }  
           else  
           {  
                fprintf(file_pointer, "\n%s", password);  
                printf("\n\n~password bookmarked successfully!~\n\n");  
   
                fclose(file_pointer);  
           }  
      }  
 };  
   
 class Display  
 {  
      int length;  
      char input, invalid;  
      char catch_password[100];  
      Mechanism mech;  
   
 public:  
   
      void banner(void)  
      {  
           printf("\n\n\nPASSWORD GENERATOR V.1.0\n");  
           printf("-----------------------------------\n\n");  
      }  
   
      void main_menu(void)  
      {  
           clear();  
   
           banner();  
   
           printf("1.Generate a new password\n");  
           printf("2.Bookmarked passwords\n");  
           printf("3.exit");  
   
           printf("\n\nyour selection : ");  
           scanf_s(" %c", &input, 1);  
   
           switch (input)  
           {  
           case '1':  
                generate_menu_1();  
                break;  
           case '2':  
                bookmark_display();  
                break;  
           case '3':  
                exit (0);  
           default:  
                clear();  
                printf("invalid entry. Enter any key to retry : ");  
                scanf_s(" %c", &invalid, 1);  
                main_menu();  
           }  
      }  
   
      void generate_menu_1(void)  
      {  
           clear();  
   
           banner();  
   
           printf("1.Classic password :\n\n");  
           printf("\tA Classic password generates password from pre-defined characters A-Z, a-z, 0-9 and the symbols (space) ! # $ %% & ' ( ) * + , - . / \\ \" : ; < = > ? @ [ ] ^ _ ` { | } ~. These passwords are not accepted by all websites but only a few.");  
           printf("\n\n\n");  
           printf("2.Custom password : \n\n");  
           printf("\tA Custom password generates a password from the characters that you input! Just mention all the characters accepted by the website and you'll have your password generated");  
   
           printf("\n\nyour selection : ");  
           scanf_s(" %c", &input, 1);  
   
           switch (input)  
           {  
           case '1':  
                generate_menu_2(0);  
                break;  
           case '2':  
                generate_menu_2(1);  
                break;  
           default:  
                clear();  
                printf("invalid entry. Enter any key to retry : ");  
                scanf_s(" %c", &invalid, 1);  
                generate_menu_1();  
           }  
      }  
   
      void generate_menu_2(int type)  
      {  
           char password_type[2][15] = { "Classic","Custom" };  
           clear();  
   
           banner();  
        
           printf("\n\nenter length of password required (max 100): ");  
           scanf_s("%d", &length);  
   
           if (length > 100)  
           {  
                clear();  
                printf("invalid entry. Enter any key to retry (password length must be < 100) : ");  
                scanf_s(" %c", &invalid, 1);  
                generate_menu_2(type);  
           }  
   
           strcpy_s(catch_password, mech.password_generator(type, length));  
           printf("\n\n* %s password generated : %s\n\n", password_type[type],catch_password);  
   
           printf("r -> regenerate || b -> bookmark || x -> exit to menu");  
   
           printf("\n\nyour selection : ");  
           scanf_s(" %c", &input, 1);  
   
           switch (input)  
           {  
           case 'r':  
                generate_menu_2(type);  
                break;  
           case 'b':  
                mech.bookmark_password(catch_password);  
                printf("\n\nEnter any key to go to bookmarks : ");  
                scanf_s(" %c", &input, 1);  
                bookmark_display();  
                break;  
           case 'x':  
                main_menu();  
                break;  
           default:  
                clear();  
                printf("invalid entry. Enter any key to retry (password will be changed) : ");  
                scanf_s(" %c", &invalid, 1);  
                generate_menu_2(type);  
           }  
      }  
   
      void bookmark_display(void)  
      {  
           FILE *file_pointer;  
           char ch;  
   
           fopen_s( &file_pointer, "password_bookmarks.txt", "r");  
   
           if (file_pointer == NULL)  
           {  
                clear();  
                printf("file opening error. Enter any key to exit to main menu : ");  
                main_menu();  
           }  
   
           clear();  
   
           banner();  
   
           printf("\t BOOKMARKS: \n\n");  
   
           for (int index = 0; ( ch = fgetc(file_pointer) ) != EOF; )  
           {  
                if (ch == '\n')  
                {  
                     index++;  
                     printf("\n%d. ", index);  
                }  
                else  
                {  
                     printf("%c", ch);  
                }  
           }  
   
           fclose(file_pointer);  
   
           printf("\n\nc -> clear bookmarks || any_key -> exit to menu ");  
             
           printf("\n\nyour selection : ");  
           scanf_s(" %c", &input, 1);  
   
           if(input == 'c')  
           {  
                FILE *file_pointer;  
                fopen_s(&file_pointer, "password_bookmarks.txt", "w");  
   
                if (file_pointer == NULL)  
                {  
                     printf("file opening error. press any key to exit to menu : ");  
                     scanf_s(" %c", &ch, 1);  
                     main_menu();  
                }  
   
                clear();  
                printf("All bookmarked passwords cleared. press any key to exit to menu : ");  
   
                scanf_s(" %c", &input, 1);  
   
                fclose(file_pointer);  
           }  
   
           main_menu();  
      }  
 };  
   
 int main(void)  
 {  
      srand((unsigned int)(time(NULL)));  
   
      class Display obj;  
   
      obj.main_menu();  
 }  

So if you have visual studio 2015 then you can just copy paste the source code (don't forget to change stdio.h to stdafx.h) and build the application :)

Here are some screenshots:

main menu

password type selection menu

password generation

bookmarking password

bookmarked passwords

Comments

  1. Simply superbbbb dude finally u have done it, Awesome!!!

    ReplyDelete

Post a Comment

Popular posts from this blog

Beginner's guide to Solving the N-Queens problem using backtracking method

PvP Chain reaction game using Python

Guide to Solving Maximal Subarray Problem using Kadane's Algorithm