Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem: Write a C++ program that will manage passwords. Your program will conta

ID: 3680518 • Letter: P

Question

Problem: Write a C++ program that will manage passwords.

Your program will contain:

• A Class, PasswordManager, that will manage a single password.

• A main function, that will allow the user to test the PasswordManager class.

Your program should consist of the following files:

PasswordManager.h

PasswordManager.cpp

PasswordDriver.cpp (containing the main function)

You should also have a makefile that can be used to build the executable program.

Note: a password does not contain any whitespace. When a password is being entered by the user, assume a whitespace character indicates the end of the password.

PasswordManager Class:

The PasswordManager class should have just one member variable, which will store the encrypted password (a string). Do not store the password unencrypted!

The PasswordManager class should have the following two internal member functions (not accessible outside of the class):

encrypt: this takes a password (a string) and returns the encrypted form of the password. Note: there is no decrypt function (there is no need to decrypt passwords). We will use the following VERY simple encryption algorithm (a Caesar Cipher):

For every character in the input string, add 10 to the ascii value of the character. The encrypted character’s ascii value must stay in the range of printable, nonwhitespace characters: 33 to 126. This can be enforced using this formula:

ascii value of encrypted char = ((ascii value of ch - 33) + 10) % 94 + 33

Store all the resulting chars in a string to be returned as the result of the function (hint: use the string.append function, or + or +=).

verifyPassword: this takes a string (a password) and returns true if it meets the following criteria:

• it is at least 8 characters long

• it contains at least three out of the following four types of characters:

- Uppercase letters

- Lowercase letters

- Numbers

- Symbols

Otherwise it returns false.

The PasswordManager should have the following member functions that are accessible outside of the class:

setEncryptedPassword: (a setter function) takes a string (an encrypted password) and stores it in the member variable. getEncryptedPassword: (a getter function) returns the value of the encrypted password stored in the member variable.

setNewPassword: takes a string (a proposed password). If it meets the criteria in verifyPassword, it encrypts the password and stores it in the member variable and returns true. Otherwise returns false.

validatePassword: takes a string (a password) and returns true if, once encrypted, it matches the encrypted string stored in the the member variable. Else returns false.

Input/Output:

The main function should create and use one instance of the PasswordManager class. It is called “the password manager” below.

Your main function will use a file “password.txt” to store the encrypted password in between executions of the program. However, the file may not yet exist the first time the program is executed. So when your main function starts, it should first try to input an encrypted password from the file “password.txt”. If the file exists and contains a string, the program should set the encrypted password in the password manager. Otherwise it should set the password in the password manager to “abc123@@@”.

Your program will use the following menu to prompt the user to test the implementation:

Password Utilities:

A. Change Password

B. Validate Password

C. Quit Enter your choice:

The menu should be processed in a loop, so the user may continue testing the password operations.

The Change Password option should ask the user to enter a new password, and explain the criteria for a valid password. The main function should call the password manager to verify and change the password. It should output a message indicating whether or not the password was changed. If it was not changed, it should NOT repeat and ask the user to try again.

The Validate Password option should ask the user to input the password. Then the main function should call the password manager to validate the password, and then the main function should output whether or not the password was valid (matching the one stored by the password manager) or not. If it was not valid, it should NOT repeat and ask the user to try again.

When the user selects C to quit, the program should save the encrypted password in the file “password.txt” (overwriting anything that was previously in the file).

NOTES:

• Do NOT change the names of the functions! Use the exact same function names, and do not change the case (uppercase/lowercase). DO NOT change the menu choice letters. • Create and use a makefile to compile the executable program. Modify the one from the lecture. I recommend calling the executable file “password”.

• Put the Class declaration in the header file, the implementation of the class member functions in PasswordManager.cpp and the main function in PasswordDriver.cpp. Put a header comment at the top of each file.

• ALL of the input and output must be done by the driver. The password manager class should not do ANY input/output, not to the screen OR the file!

• constructor functions are NOT required. • Your program must compile and run, otherwise you will receive a score of 0.

• Your program must pass Test Case 0 or you will receive a score of 30 or less with no credit for the other grading categories (correctness/constraints/style). The input values and expected output are in a file called TC0.txt on the class website. This test case changes the password, then attempts to validate the new password (the password does not need to be saved to a file to pass TC0). Your program must contain a PasswordManager class to pass TC0.

Logistics:

Since there are multiple files for this assignment, you need to combine them into one file before submitting them. You should use the zip utility from the Linux/Unix command line:

[...]$zip assign4_xxxxx.zip PasswordDriver.cpp

PasswordManager.cpp PasswordManager.h makefile

This combines the 4 files into one zip file, assign4_xxxxx.zip (where xxxxx is your NetID). Then you should submit only assign4_xxxxx.zip.

Explanation / Answer

PasswordManager.h
#include <string>
using namespace std;
class PasswordManager
{
private:
        string encrypt(string);
        bool verifyPassword(string);
      
public:
       bool setNewPassword(string);
       bool validatePassword(string);
       string getEncryptedPassword();
       void setEncryptedPassword(string);
};

driver.cpp

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "PasswordManager.h"

using namespace std;

int main()
{
   PasswordManager pm;

   char choice;
   string newpassword;
   string origPass = "ABCabc123";

   ifstream fin;
   fin.open("Passwordfile.txt");
   if(!fin)
       pm.setNewPassword(origPass);


   do{
       cout << "Password Utilities:" << endl
           << "a. Change Password" << endl
           << "b. Validate Password" << endl
           << "c. Quit" << endl
           << "Enter your choice: ";
       cin >> choice;
   switch (choice)
   {


      case 'a' :
              cout << "New password must:" << endl
                   << " - be at least 6 characters long" << endl
                   << " - contain at least 1 uppercase letter" << endl
                   << " - contain at least 1 lowercase letter" << endl
                   << " - contain at least 1 digit" << endl
                   << "Enter new password: ";
              cin >> newpassword;
              if(pm.setNewPassword(newpassword) == true)
              {
                  cout << "Password changed" << endl << endl;
              }

              else
                  cout << "Invalid password entered" << endl;

          break;

      case 'b' :
              cout << "Enter your password" << endl;
              cin >> newpassword;

              if(pm.validatePassword(newpassword) == true)
                  cout << "Valid password entered" << endl;
              else
                  cout << "Invalid password entered" << endl;

          break;

      case 'c' :
          cout << "Exiting Application!" << endl << endl;
             return 0;
   }

   }while(choice != 'c');

}

passwordManager.cpp
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "PasswordManager.h"

using namespace std;

string PasswordManager::encrypt(string password)
{
       string encryptPass;
       for(int i = 0; i < password.length(); i++)
             encryptPass.append(1,password[i] ^ '2'); //a = password[i] ^ '2';
       return encryptPass;
}

bool PasswordManager::verifyPassword(string password)
{
     int passSize = 0;
     int passUpper = 0;
     int passLower = 0;
     int passDigit = 0;

   bool passFail = false;

           if( password.size() >= 6)
           {
               passSize++;
           }

     for(int i = 0; i < password.length(); i++)
     {

           if(isupper(password[i]))
           {
               passUpper++;
           }
           if(islower(password[i]))
           {
               passLower++;
           }
           if(isdigit(password[i]))
           {
               passDigit++;
           }
     }

     if(passSize >= 1 && passUpper >= 1 && passLower >= 1 && passDigit >= 1)
           {
               passFail = true;
           }

     return passFail;
}

void PasswordManager::setEncryptedPassword(string encryptedPass)
{
     ofstream fout;
     fout.open("Passwordfile.txt");
     fout << encryptedPass;
     fout.close();
}

string PasswordManager::getEncryptedPassword()
{
       string encryptedPass;
       ifstream fin;
       fin.open("Passwordfile.txt");
       fin >> encryptedPass;
     
       fin.close();
       return encryptedPass;
}

bool PasswordManager::setNewPassword(string password)
{
     bool passFail = false;

     if(verifyPassword(password) == true)
     {
         setEncryptedPassword(encrypt(password));
         passFail = true;
     }

     return passFail;
}

bool PasswordManager::validatePassword(string password)
{
     bool passFail= false;
     int count = 0;
     string newpass;
     string oldpass;
   
     oldpass = getEncryptedPassword();
   
     newpass = encrypt(password);

     for(int i = 0;i < newpass.length(); i++)
           if( newpass[i] == oldpass[i])
               count++;
     if(count >= newpass.length())
               passFail = true;
             
     return passFail;
}

output


Password Utilities:                                                                                                                                         
a. Change Password                                                                                                                                          
b. Validate Password                                                                                                                                        
c. Quit                                                                                                                                                     
Enter your choice: a                                                                                                                                        
New password must:                                                                                                                                          
- be at least 6 characters long                                                                                                                            
- contain at least 1 uppercase letter                                                                                                                      
- contain at least 1 lowercase letter                                                                                                                      
- contain at least 1 digit                                                                                                                                 
Enter new password: Jerry123                                                                                                                                
Password changed                                                                                                                                            
Password Utilities:                                                                                                                                         
a. Change Password                                                                                                                                          
b. Validate Password                                                                                                                                        
c. Quit                                                                                                                                                     
Enter your choice: b                                                                                                                                        
Enter your password                                                                                                                                         
Jerry123                                                                                                                                                    
Valid password entered                                                                                                                                      
Password Utilities:                                                                                                                                         
a. Change Password                                                                                                                                          
b. Validate Password                                                                                                                                        
c. Quit                                                                                                                                                     
Enter your choice: c                                                                                                                                        
Exiting Application!                                                                                                                                        
                                                                                                                                                            

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote