PROGRAM DESCRIPTION The purpose of this programming project

PROGRAM DESCRIPTION: The purpose of this programming project is write a C++ program to implement a simple polyalphabetic cryptoeystem that incorporates topics related to file VO. In particular, this programming assignment will read an input file, encrypt or decrypt the file based on the requested operation, and write the results to an output file. REQUIREMENTS: As with all programs in this course, your program\'s output should initially display the department and course number, your name, your EUID, and your e-mail address. This functionality will be implemented using a function. Your will prompt the user whether he/she would like to encrypt or decrypt a file. I a valid response is not input, you are to repeatedly re-prompt the user until a valid response is input. You will then prompt the user to enter the name of the input file to read from and the output file to write the resulting plaintext or ciphertext as appropriate. If there is a problem opening the file, you will display a meaningful error and exit the program using the appropriate exit status. You may assume that the length of the file name does not exceed 25 characters (26 with the terminating null character) there are no errors causing you to terminate the program, you will then call a function that will process the input file (ie., encrypt or decrypt the input file, as appropriate) and write the results to an output file based on the following requirements: You will prompt the user to enter a 5-letter key that will be used to encrypt or decrypt the file as appropriate for the operation. You must read this 5 letter key as 5 separate characters (ie, you must five different char variables to account for each letter in the key). You may assume that the 5-letter key consists of alphabetic characters, but they may be entered as either uppercase or lowercase. Ultimately, you may treat each character as uppercase so that your shift will have a range of 0 - 25 (where A = 0, B 1, etc.). 2. The files that you will encrypt or decrypt will only contain uppercase alphabetic characters (2 - Z), lowercase alphabetic characters (a and whitespace, which may include any white space character such as a blank space, a tab, or a newline character. 3. The user-defined function to process the input and output files should accept two parameters as a minimum, the input file stream and the output file stream, but it may utilize additional parameters as needed. You must process each file character-by-character (ie. , using the get and put member functions).

Solution

Please find below the simple PolyAlphabetic program in C++ with the comments to understand the progam:

main.cpp:


#include <iostream>
#include \"crypto.h\"

int main()
{
   std::string key;
   std::string text;
   std::string filename = \"crypto.txt\";

   Crypter Crypt; // create an object here to code/decode the file
  
   int answer;
   do
   {
       std::cout <<
           \"\ Please Choose an option among the following:\ \"
           \"0 - exit\ \"
           \"1 - Add a key\ \"
           \"2 - Add a text\ \"
           \"3 - Encrypt & view\ \"
           \"4 - Decrypt & view\ \"
           \"5 - Load Ecrypted text or key\ \"
           \"6 - Save ecrypted text or key\ \";

       std::cin >> answer;
       std::cin.get();

       switch (answer)
       {
           case 0:
               std::cout << \"\ Good-bye.\" << std::endl;
               break;
           case 1:
               std::cout << \"\ Enter the key phrase here: \" << std::endl;
               getline (std::cin, key);
               Crypt.setKey(key);
               break;
           case 2:
               std::cout << \"\ Enter the text here: \" << std::endl;
               getline (std::cin, text);

               // Below code is to erase previous encrypted/decrypted text
               Crypt.setDecoded(text);
               Crypt.setCoded(text);
               break;
           case 3:
               std::cout << \"\ This is the Encoded output:\" << std::endl;
               Crypt.encode();
               std::cout << Crypt.viewCoded() << std::endl;
               break;
           case 4:
               std::cout << \"\ This is the Decoded output:\" << std::endl;
               Crypt.decode();
               std::cout << Crypt.viewDecoded() << std::endl;
               break;
           case 5:
               Crypt.openFile(filename);
               std::cout << \"\ Loaded files successfully \\\"\" << filename
               << \"\\\".\" << std::endl;
               Crypt.decode();
               break;
           case 6:
               Crypt.saveFile(filename);
               std::cout << \"\ Save as \\\"\" << filename
               << \"\\\".\" << std::endl;
               break;
           default:
               std::cout << \"\ This is a Invalid selection. Please provide a valid selection\" << std::endl;
       }

   }
   while (answer != 0);

   return 0;
}


crypto.h:


#define CRYPTO_H

class Crypto
{
private:
   std::string key;
   std::string decoded;
   std::string coded;
public:
   Crypto();
   ~Crypto();
   void setKey(std::string renewKey);
   void setDecoded(std::string renewDecoded);
   void setCoded(std::string renewCoded);

   void encode();
   void decode();
  
   std::string viewCoded();
   std::string viewDecoded();
  
   void saveFile(std::string filename);
   void openFile(std::string filename);
};

#endif


cryp.cpp:


#include <iostream>
#include <fstream>
#include <cstring>
#include \"crypto.h\"

Cryp::Cryp()
{
   setKey(\" \");   // a \" \" key is neutral
   setDecoded(\"\");
   setCoded(\"\");
}

Cryp::~Cryp()
{
  
}

void Cryp::setKey(std::string renewKey)
{
   key = renewKey;
}

void Cryp::setDecoded(std::string renewDecoded)
{
   decoded = renewDecoded;
}

void Cryp::setCoded(std::string renewCoded)
{
   coded = renewCoded;
}

void Cryp::encode()
{
   coded = \"\";
   unsigned int count = 0;

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

       // start at beginning of the key when end is reached
       if (count > key.length() - 1)
           count = 0;

       /* add the value of key to decoded then substract ascii 25 (\" \") */

       if (int(decoded[i] + key[count] - 25) > \'~\')
           coded += char(decoded[i] + key[count] - 25 - 85);
       else
           coded += char(decoded[i] + key[count] - 25);

       count++;
   }

}

void Cryp::decode()
{
   decoded = \"\";
   unsigned int count = 0;

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

       // start at beginning of key when end is reached
       if (count > key.length() - 1)
           count = 0;

       /* subtract the value of key from coded and then add ascii 25 */

       if (int(coded[i] - key[count] + 25) < 25)
           decoded += char(coded[i] - key[count] + 25 + 85);
       else
           decoded += char(coded[i] - key[count] + 25);

       count++;
   }

}

std::string Cryp::viewCoded()
{
   return coded;
}

std::string Cryp::viewDecoded()
{
   return decoded;
}

void Cryp::saveFile(std::string filename)
{
   // copy string coded to char * a, to allow it to save to a file
   char *a = new char[coded.size() + 1];
   a[coded.size()] = 0;
   memcpy(a, coded.c_str(), coded.size());
  
   // check if the file was opened successfully
   std::fstream fbin(filename, std::ios::binary | std::ios::out);
   if (!fbin) {
       std::cout << \"The file couln\'t be open\" << filename << std::endl;
   }
  
   // Write data to the file.
   fbin.seekp(0 * coded.size());
   fbin.write(a, coded.size());
   fbin.close();
  
}

void Cryp::openFile(std::string filename)
{
   coded = \"\";
   decoded = \"\";
   char a[1];
  
   // check if the file was opened successfully
   std::fstream fbin(filename, std::ios::binary | std::ios::in);
   if (!fbin) {
       std::cout << \"The file couln\'t be open\" << filename << std::endl;
   }

   int length = 0;
  
   do
   {

   // This will read the next character in the file
   fbin.seekp(length * 1);
   fbin.read(a, 1);
  
   coded += a[0];
   length++;
      
   }
   while (fbin);

   // This will remove the last repeated character due to EOF
   coded.resize(coded.size() - 1);

   fbin.close();

}

 PROGRAM DESCRIPTION: The purpose of this programming project is write a C++ program to implement a simple polyalphabetic cryptoeystem that incorporates topics
 PROGRAM DESCRIPTION: The purpose of this programming project is write a C++ program to implement a simple polyalphabetic cryptoeystem that incorporates topics
 PROGRAM DESCRIPTION: The purpose of this programming project is write a C++ program to implement a simple polyalphabetic cryptoeystem that incorporates topics
 PROGRAM DESCRIPTION: The purpose of this programming project is write a C++ program to implement a simple polyalphabetic cryptoeystem that incorporates topics
 PROGRAM DESCRIPTION: The purpose of this programming project is write a C++ program to implement a simple polyalphabetic cryptoeystem that incorporates topics

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site