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

Write a password management program in C++. It helps manage users’ passwords in

ID: 3803758 • Letter: W

Question

Write a password management program in C++. It helps manage users’ passwords in an
encrypted vault. Users are able to insert, view and delete passwords using the program. To
enhance the security, each password, before being stored in the program, can be encrypted
using either one of the following techniques:
1. Caesar Cipher

An encryption/decryption technique that encodes English letters based on a key value
which is between 1 and 25. For example, when the plaintext is “axe” and the key is 5,
the first letter ‘a’ will shift 5 letters to the right (abcdef) and become ‘f’.
The second letter ‘x’ will become ‘c’ (xyzabc). The third letter ‘e’ will
become ‘j’. The ciphertext is “fcj”. For decryption, the shifting is in the opposite
direction.
COMP1011 Programming Fundamentals 2016/17 Semester 2
Department of Computing
The Hong Kong Polytechnic University
- 2 -
2. Monoalphabetic Cipher
An encryption/decryption technique that encodes English letters based on a key,
which is a permutation of the 26 English letters. For example, when the key is
“pohmlwtuksdcinjgbzaryqfexv”, the plaintext letter, ‘a’, will be encrypted to ‘p’. ‘b’
will be encrypted ‘o’ and so on. Using the same key, if the plaintext is “axe”, the
ciphertext will be “pel”. For decryption, we look up each ciphertext letter and find out
its position in the key, which represents the corresponding letter in the alphabet
ordering. For example, ‘p’ is the first letter in the key, so ‘a’ is the plaintext. ‘e’ is the
24th letter, so ‘x’ is the plaintext.


Program Details

When the program is run, a list of functions is displayed, as shown below:


Insert a new password. (I)
This function first prompts the user for an encryption mode:
1. No encryption
2. Caesar Cipher
3. Monoalphabetic Cipher
Then, it prompts the user for a description of the password. If Caesar Cipher or
Monoalphabetic Cipher is selected, the user has to input a key according to the cipher
specification. Finally, the user has to input a password, which is to be stored in the program

The password has to be encrypted according to the encryption mode. A sample screen is
shown below:


View a password. (V)
The function first shows a list of password descriptions. Each entry consists of entry number,
password description and encryption mode. The user chooses one of the entries and if the
password is encrypted using Caesar Cipher or Monoalphabetic Cipher, the user has to enter
the key for decryption. The password will then be displayed to the user. A sample screen is
shown below:


Note: Display “No password is stored.” if no password has been inserted to the program.
Delete a password. (D)
This function first shows a list of password descriptions. The user chooses one of the entries
and the corresponding password is removed from the program. A sample screen is shown
below:


Note: Display “No password is stored.” if no password has been inserted to the program.
Exit the program. (E)
The program is terminated. “Thank you for using the program!” is displayed on the screen.
Note: If any one of the functions (except “Exit the program”) is completed, the program will
display the function menu again.

Welcome to Personal Password Management Center Please choose one of the options Insert a new password (I) (V) view a password Delete a password. (D) Exit the program. (E) Your input

Explanation / Answer

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;


typedef struct pass
{
   char desc[100];
   char encryptMethod[50];
   char encryptedPassword[100];
   struct pass* next;
}password_t;

void caesarEncrypt(password_t*,int key, char* password);
void monoalphaEncrypt(password_t*, char* key, char* password);


void caesarDecrypt(password_t*, int key, char* result);
void monoalphaDecrypt(password_t*, char* key, char* result);


int main()
{
   password_t* list = NULL,*p = NULL,*itr = NULL, *temp = NULL;
char choice = ' ';
   char password[100] = {''};
char char_key[100] = {''};
  
   int in = 0, int_key = 0, i=0, j=0;
  
   cout << "Welcome to Personal Password Management Center!" <<endl;
   while((choice != 'E') && (choice != 'e'))
   {
       cout<<"Please choose one of the options:" <<endl;
       cout<<"Insert a new password. (I)"<<endl;
       cout<<"View a password. (V)"<<endl;
       cout<<"Delete a password. (D)"<<endl;
       cout<<"Exit a program. (E)"<<endl;
cout<<endl<<"Your input: ";

       cin>>choice;
          
       switch(choice)
       {
           case 'I':
           case 'i':
                   p = new password_t;
                   memset(p,0,sizeof(password_t));
                  
                   cout<<"Please choose an encryption mode:"<<endl;
                   cout<<"1.No encryption"<<endl;
                   cout<<"2.Caesar Cipher"<<endl;
                   cout<<"3.Monoalphabetic Cipher"<<endl;
                   cout<<"Your input:";
                  
                   cin>> in;
                   cout<<"Please enter the description of the password: ";
                   cin>> p->desc;
                  
                  
                   if(in == 2){
                      
                       cout<<"Please enter a key [1 - 25]: ";
                       cin>> int_key;
                                              
                       snprintf(p->encryptMethod, sizeof(p->encryptMethod),
                               "%s","Caesar cipher");
                      
                       cout<<"Please enter the password:";
                       cin>>password;
                      
                       caesarEncrypt(p,int_key,password);
                   }
                   else if(in == 3){
                      
                       cout<<"Please enter a key: ";
                       cin>> char_key;
                      
                       snprintf(p->encryptMethod, sizeof(p->encryptMethod),
                               "%s","Monoalphabetic Cipher");
                      
                       cout<<"Please enter the password:";
                       cin>>password;
                      
                       monoalphaEncrypt(p,char_key,password);
                   }
                   else{
                       snprintf(p->encryptMethod, sizeof(p->encryptMethod),
                               "%s","No Encryption");
                                 
                       cout<<"Please enter the password:";
                       cin>>p->encryptedPassword;
                   }
                  
                   if(list == NULL)
                   {
                       list = itr = p;
                      
                   }
                   else{
                       itr->next = p;
                       itr = itr->next;
                   }
                  
                   break;
                  
           case 'V':
           case 'v':
                   if(list == NULL){
                       cout<<"No password is stored"<<endl;
                       break;
                   }
                   temp = list;
                   i=1;
                  
                   cout<<"Please choose one of the following:"<<endl;
                   while(temp != NULL)
                   {
                       cout<<i << " " << temp->desc << " " << temp->encryptMethod<<endl;
                       i++;
                       temp = temp->next;
                   }
                   cout<<"Your input: ";
                   cin>>in;
                  
                   temp = list;
                  
                   j=1;
                   while(j < in){
                       temp = temp->next;
                       j++;
                   }
                   char result[100];
                  
                   if(!strcmp(temp->encryptMethod,"Caesar cipher")){
                           cout<<"Enter the key [1-25] for decryption: ";
                           cin>>int_key;
                          
                           caesarDecrypt(temp,int_key,result);
                           cout<<"The password is : "<< result<<endl;
                   }
                   else if(!strcmp(temp->encryptMethod,"Monoalphabetic Cipher"))
                   {
                       cout<<"Enter the key for decryption: ";
                       cin>> char_key;

                       monoalphaDecrypt(temp,char_key,result);
                       cout<<"The password is : "<< result<<endl;
                   }
                   else{
                       cout<<"The password is : "<< temp->encryptedPassword<<endl;
                   }
                   break;
                  
           case 'D':
           case 'd':
                   if(list == NULL){
                       cout<<"No password is stored"<<endl;
                       break;
                   }
                   temp = list;
                   i=1;
                  
                   cout<<"Please choose one of the following:"<<endl;
                   while(temp != NULL)
                   {
                       cout<<i << " " << temp->desc << " " << temp->encryptMethod<<endl;
                       i++;
                       temp = temp->next;
                   }
                   cout<<"Your input: ";
                   cin>>in;
                  
                   p= temp = list;
                  
                   j=1;
                   while(j < in){
                       p = temp;
                       temp = temp->next;
                       j++;
                   }
                  
                   if(temp == list){
                      
                       list = list->next;
                       delete temp;
                   }
                   else{
                       p->next = temp->next;
                       temp->next = NULL;
                       delete temp;
                   }
                  
                   break;
                  
                  
           case 'E':
           case 'e':
                   cout<<"Thank you for using the program!"<<endl;
                   return 0;
          
           default:
                   cout<<"Incorrect choice.. Try again"<<endl;
                   break;
          
       }      
   }

}


void caesarEncrypt(password_t* pass,int key, char* password)
{
   int len = 0,i =0;
   int val = ' ';
  
   if(pass == NULL)
       return ;
  
   len = strlen(password);
  
for(i=0;i< len;i++){
      
       if(password[i] >=65 && password[i] <=90){
          
           val = password[i] + key;
           if(val > 90){
               val = val -90;
               pass->encryptedPassword[i] = 'A' + val - 1;
           }
           else{
               pass->encryptedPassword[i] = (char)val;
           }
       }
      
       if(password[i] >=97 && password[i] <=122){
          
           val = password[i] + key;
           if(val > 122){
        val = val - 122;
               pass->encryptedPassword[i] = 'a' + val - 1;
           }
           else{
               pass->encryptedPassword[i] = (char)val;
           }
       }
   }
   pass->encryptedPassword[i] = '';
  
}


void monoalphaEncrypt(password_t* pass, char* key, char* password)
{
   int i=0, len=0, pos = 0;
   char val = ' ';
  
   if(pass == NULL){
       return;
   }
  
   len = strlen(password);
  
   for(i=0; i<len;i++)
   {
      
       if(password[i] >=65 && password[i] <=90){
          
           pos = password[i] - 65;
           pass->encryptedPassword[i] = key[pos];
       }
      
       if(password[i] >=97 && password[i] <=122){
      
           pos = password[i] - 97;
           pass->encryptedPassword[i] = key[pos];
       }
      
   }
   pass->encryptedPassword[i] = '';  
}


void caesarDecrypt(password_t* pass, int key, char* result)
{
   int len = 0,i =0;
   int val = ' ';
  
   if(pass == NULL)
       return ;
  
   len = strlen(pass->encryptedPassword);
  
for(i=0;i< len;i++){
      
       if(pass->encryptedPassword[i] >=65 && pass->encryptedPassword[i] <=90){
          
           val = pass->encryptedPassword[i] - key;
           if(val < 65){
               val = val - 65;
               result[i] = 'Z' + val + 1;
           }
           else{
               result[i] = (char)val;
           }
       }
      
       if(pass->encryptedPassword[i] >=97 && pass->encryptedPassword[i] <=122){
          
           val = pass->encryptedPassword[i] - key;
           if(val < 97){
               val = val - 97;
               result[i] = 'z' + val + 1;
           }
           else{
               result[i] = (char)val;
           }
       }
   }
   result[i] = '';
  
}
void monoalphaDecrypt(password_t* pass, char* key, char* result)
{
   int i=0, len=0, pos = 0, j =0 ;
   char val = ' ';
  
   if(pass == NULL){
       return;
   }
  
   len = strlen(pass->encryptedPassword);
  
   for(i=0; i<len;i++)
   {
      
       if(pass->encryptedPassword[i] >=65 && pass->encryptedPassword[i] <=90){
          
           for(j=0;j<26;j++){
               if(key[j] == pass->encryptedPassword[i])
                   break;
           }
           result[i] = 'A' + j;
       }
      
       if(pass->encryptedPassword[i] >=97 && pass->encryptedPassword[i] <=122){
      
           for(j=0;j<26;j++){
               if(key[j] == pass->encryptedPassword[i])
                   break;
           }
           result[i] = 'a' + j;
       }
      
   }
   result[i] = '';  
  
}

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