0

This is a code that i modified a bit to generate a password and save it with a string for a name and i would like for it to save the passwords to be viewed whenever the code is reopened any help would be greatly appreciated. I would like for it to be able to self modify itself so that i can display and delete the passwords that are generated

#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>

using namespace std;

//selectArray is  a utility function that is used to
//randomly generate a integer in the range 1 to 4 (both inclusive)
int selectArray()
{
    // Using malloc() function we are Dynamically allocating memory
    //for a Integer and this function will return a pointer pointing to the allocated memory
    int *j=(int*)malloc(sizeof(int));
    // The content of int pointer j (i.e an address) will be
    //interpreted as an Integer and will be assigned to integer variable i.
    int i=int(j);
    i=i%5;
    if(i==0)
        i++;
    return i;
}

//getKey() is another utility function that is used to randomly generate
//an integer in the range 0 to 25 (both inclusive)
int getKey()
{
    // Using malloc() function we are Dynamically allocating memory for an
    //Integer and this function will return a pointer pointing to the allocated memory
    int *i=(int *)malloc(sizeof(int));
    // The content of int pointer i (i.e an address) will be interpreted as
    // an Integer and will be assigned to integer variable key.
    int key=int(i);
    key=key%26;
    return key;
}
char generate_password(int length)
{
    //Intializing result string password as NULL.
    string password="";

    //Strings whose characters will be used to build password
    string alphabet="abcdefghijklmnopqrstuvwxyz";
    string ALPHABET="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string s_symbol="!@#$%&";
    string number="0123456789";
    string str, strresult;
    string str1 = "=";


    //intializing local variables
    int key,count_alphabet=0,count_ALPHABET=0,count_number=0,count_s_symbol=0;

    //Count will store the length of the password being created,initially this will be zero(0)
    int count=0;
    while(count<length)
    {
        // selectArray() function will return a number 1 to 4
        // and will use to select one of the above defined string
        //(i.e alphabet or ALPHABET or s_symbol or number )
        // 1 is for string alphabet
        // 2 is for string ALPHABET
        // 3 is for string number
        // and 4 is for string s_symbol


        int k=selectArray();

        //for the first character of password it is mentioned that ,it should be a letter
        // so the string that should be selected is either alphabet or ALPHABET (i.e 1 or 2)
        // following if condition will take care of it.
        if(count==0)
        {
            k=k%3;
            if(k==0)
                k++;
        }
        switch(k)
        {
            case 1:
            // following if condition will check if minimum requirement of alphabet
            // character has been fulfilled or not
            // incase it has been fulfilled and minimum requirements of other
            // characters is still left then it will break ;
            if((count_alphabet==2)&&(count_number==0||count_ALPHABET==0||count_ALPHABET==1||count_s_symbol==0))
                break;

            key=getKey();
            password=password+alphabet[key];
            count_alphabet++;
            count++;
            break;

            case 2:
            // following if condition will check if minimum requirement of
            // ALPHABET character has been fulfilled or not
            // incase it has been fulfilled and minimum requirements of
            // other characters is still left then it will break ;
            if((count_ALPHABET==2)&&(count_number==0||count_alphabet==0||count_alphabet==1||count_s_symbol==0))
                break;
            key=getKey();
            password=password+ALPHABET[key];
            count_ALPHABET++;
            count++;
            break;

            case 3:
            // following if condition will check if minimum requirement
            // of Numbers  has been fulfilled or not
            // incase it has been fulfilled and minimum requirements of
            // other characters is still left then it will break ;
            if((count_number==1)&&(count_alphabet==0||count_alphabet==1||count_ALPHABET==1||count_ALPHABET==0||count_s_symbol==0))
                break;

            key=getKey();
            key=key%10;
            password=password+number[key];
            count_number++;
            count++;
            break;

            case 4:
            // following if condition will check if minimum requirement of
            // Special symbol character has been fulfilled or not
            // incase it has been fulfilled and minimum requirements of
            // other characters is still left then it will break ;
            if((count_s_symbol==1)&&(count_alphabet==0||count_alphabet==1||count_ALPHABET==0||count_ALPHABET==1||count_number==0))
                break;

            key=getKey();
            key=key%6;
            password=password+s_symbol[key];
            count_s_symbol++;
            count++;
            break;
        }
    }

    cout << "Enter name for password: ";
    cin >> str;

    cout << "" << strresult;
    cout << "\n-----------------------------\n";
    cout << "         Password             \n";
    cout << "------------------------------\n\n";
    cout << " " <<  password;


    cout << "\nPassword: " << str << " = " << password;
    cout <<"\n\nPress any key continue \n";

}
int main()
{
    int opt,length;
    string str, strresult;
    string str1;
    //Menu
    do
    {
        cout << "\n-----------------------------\n";
        cout << "  Random Password Generator\n";
        cout << "------------------------------\n\n";
        cout << "    1. Generate Password" << "\n";
        cout << "    2. Exit" << "\n\n";
        cout << "Press key 1 to Generate Password and key 2 to display saved passwords : ";
        cin >> opt;

        switch(opt)
        {
            case 1:



            // Length should not exceed 100 , program should show error if it exceeds



            generate_password(10);


            break;

            default:
            // If invalid option is choosen by user it will also show error
            if(opt!=2)
            {
                printf("\nInvalid choice\n");
                printf("Please Press ( 1 ) to generate password and ( 2 ) to display saved passwords.\n");
                cout <<"Press any key to try again \n";
                getchar();
            }
            break;
        }
    }while(opt!=2);
        cout << "" << strresult;

    return 0;
}
  • Unrelated but good reading: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Also, don't include the C header `stdio.h`. Include `cstdio` if you need it at all for this. – Ted Lyngmo Mar 23 '20 at 09:10
  • Also unrelated, but use the [random library](https://en.cppreference.com/w/cpp/header/random) to generate random numbers instead of relying on random locations of heap-allocations. Btw, all of those "random numbers" leak memory. And they may cause integer overflow. – Lukas-T Mar 23 '20 at 09:12
  • Do you really want to make the code self modifying (which is not easy). Why not just store the passwords seperately to the program? Or better still store password hashes seperately. – john Mar 23 '20 at 09:15
  • Simpler would be to use some additional resource file instead of using executable file (and antivirus should warn when exe is modified)... – Jarod42 Mar 23 '20 at 09:15
  • 1
    Save the passwords to a file that you read whenever the program starts. Passwords should however never be saved in a format that would let anyone be able to read the password but for a toy project it may be ok. – Ted Lyngmo Mar 23 '20 at 09:15
  • Most modern operating systems load executables into memory and mark memory containing executable code as "execute-only" which - among other things - prevents the executable code being modified. The executable file itself is also often locked to prevent modification while being executed. These behaviours are DELIBERATELY DESIGNED to prevent things like self-modifying executables that can compromise system stability. Practically, a common use-case for self-modifying executables is malware. Better to use a separate file, with appropriately encrypted content, so store passwords. – Peter Mar 23 '20 at 10:52

0 Answers0