Question
1. In this assignment, you will be implementing a program in C++ that encrypts and decrypts using a substitution cipher. Please ignore anything that doesn't
1. In this assignment, you will be implementing a program in C++ that encrypts and decrypts using a substitution cipher. Please ignore anything that doesn't have to do with substitution cipher.
2. Do not change the provided skeleton code. You may add new code to the provided files, but the existing code MUST NOT BE CHANGED. In particular, the main.cc function should not be changed AT ALL (i.e., do not change or add new code),
3. Constructor: substitution - cipher alphabet - must contain every letter in the alphabet, and only once, and they must all be lower case. It should contain no other characters (e.g., no space, punctuation, etc.). Cipher alphabet length should always be 26. The destructor should free all memory. Valgrind will be used to make sure there are no memory leaks.
i) Encrypt: Assume that the input plain text will always be a) lower case letter, b) upper case letter, or c) a space. Space should not be encrypted (i.e., if the plain text is "hello world", the encrypted text should look something like "abcde fghij". Upper case letter should be encrypted to an upper case letter (you don't have to, but you will find that meeting the decrypt requirement below will be difficult otherwise).
ii) Decrypt: This should take in the encrypted text and return the origin plain text. It should
a) retain the case (i.e., if the input plain text has an upper case letter, the decrypted plain text should have that letter as upper case.
b) retain the space - If the plain text had spaces, the decrypted text should still have those spaces in the correct place.
MAIN.CC
#include
using namespace std;
const char* const short_opt = ":hm:i:o:"; const struct option long_opt[] = { {"help", 0, NULL, 'h'}, {"method", 0, NULL, 'm'}, {"input", 1, NULL, 'i'}, {"output", 1, NULL, 'o'}, {NULL, 0, NULL, 0} };
enum write_mode { wr, ap };
void usage(char* argv); // Substitution cipher void substitution_enc(string ifile_name, string ofile_name); void load_subs_input(string ifile_name, string& cipher_alpha, string& plain_text);
// Helper function // stores the cipher text and decrypted text to an output file void store_output(string ofile_name, string cipher_text, string decrypt_text, write_mode wm);
/* Main function */ int main(int argc, char **argv) { int c = 0; string ifile_name; string ofile_name; string enc_method;
if(argc < 2) { usage(argv[0]); return 0; }
while((c = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1) { switch(c) { case 'h': usage(argv[0]); return 0; case 'm': enc_method = optarg; break; case 'i': ifile_name = optarg; break; case 'o': ofile_name = optarg; break; default: usage(argv[0]); exit(EXIT_FAILURE); } }
char method = enc_method.at(0); switch(method) { case 's': substitution_enc(ifile_name, ofile_name); break; case 'c': caesar_enc(ifile_name, ofile_name); break; case 'r': rot13_enc(ifile_name, ofile_name); break; case 'k': rk_enc(ifile_name, ofile_name); break; case 'v': vigenere_enc(ifile_name, ofile_name); break; default: cout << "Invalid encryption method: " << method << endl; exit(EXIT_FAILURE); }
return 0; }
/* Print information on how to properly use this program */ void usage(char* call) { cout << "Usage: " << call << endl; cout << "Options:" << endl; cout << "\t-h or --help Display this information" << endl; cout << "\t-m or --method [scrkv] Encryption method" << endl; cout << "\t\t s: substitution" << endl; }
/* This function encrypts and decrypts the input (plain) text using the substitution cipher and the given cipher alphabet */ void substitution_enc(string ifile_name, string ofile_name) { string cipher_alpha; string plain_text; // Read in the cipher alphabet and the plain text to encrypt // The input file for ths substitution cipher MUST have the // cipher alphabet in line 1, and the plain text to encrypt // in line 2 load_subs_input(ifile_name, cipher_alpha, plain_text);
// Create a substitution cipher class object and use it to encrypt and // decrypt the plain text Cipher my_cipher(cipher_alpha); string cipher_text = my_cipher.encrypt(plain_text); string decrypt_text = my_cipher.decrypt(cipher_text);
// Also, create a substitution cipher class object using the default // constructor to initialize it // The default initialized object should use a cipher alphabet such that // when the plain text is encrypted, the cipher text should be identical // to the plain text Cipher my_def_cipher; string def_cipher_text = my_def_cipher.encrypt(plain_text); string def_decrypt_text = my_def_cipher.decrypt(def_cipher_text);
// Store the output to files store_output(ofile_name, cipher_text, decrypt_text, wr); store_output(ofile_name, def_cipher_text, def_decrypt_text, ap); } void load_subs_input(string ifile_name, string& cipher_alpha, string& plain_text) { ifstream inFile(ifile_name);
string line; // Read in the cipher alphabet getline(inFile, line); cipher_alpha = line;
// Read in the plain text you want to encrypt getline(inFile, line); plain_text = line;
inFile.close(); }
CIPHER.CC
#include "cipher.h"
/* Cheshire smile implementation. It only contains the cipher alphabet */ struct Cipher::CipherCheshire { string cipher_alpha; };
/* This function checks the cipher alphabet to make sure it's valid */ bool is_valid_alpha(string alpha);
// ------------------------------------------------------- // Cipher implementation /* Default constructor This will actually not encrypt the input text because it's using the unscrambled alphabet */ Cipher::Cipher() { // TODO: Implement this default constructor }
/* This constructor initiates the object with a input cipher key */ Cipher::Cipher(string cipher_alpha) { // TODO: Implement this constructor }
/* Destructor */ Cipher::~Cipher() { // TODO: Implement this constructor }
/* This member function encrypts the input text using the intialized cipher key */ string Cipher::encrypt(string raw) { cout << "Encrypting..."; string retStr; // TODO: Finish this function
cout << "Done" << endl;
return retStr; }
/* This member function decrypts the input text using the intialized cipher key */ string Cipher::decrypt(string enc) { string retStr; cout << "Decrypting..."; // TODO: Finish this function
cout << "Done" << endl;
return retStr;
} // -------------------------------------------------------
// Helper functions /* Find the character c's position in the cipher alphabet/key */ unsigned int find_pos(string alpha, char c) { unsigned int pos = 0;
// TODO: You will likely need this function. Finish it.
return pos; }
/* Make sure the cipher alphabet is valid - a) it must contain every letter in the alphabet b) it must contain only one of each letter c) it must be all lower case letters ALL of the above conditions must be met for the text to be a valid cipher alphabet. */ bool is_valid_alpha(string alpha) { bool is_valid = true; if(alpha.size() != ALPHABET_SIZE) { is_valid = false; } else { unsigned int letter_exists[ALPHABET_SIZE]; for(unsigned int i = 0; i < ALPHABET_SIZE; i++) { letter_exists[i] = 0; } for(unsigned int i = 0; i < alpha.size(); i++) { char c = alpha[i]; if(!((c >= 'a') && (c <= 'z'))) { is_valid = false; } else { letter_exists[(c - 'a')]++; } } for(unsigned int i = 0; i < ALPHABET_SIZE; i++) { if(letter_exists[i] != 1) { is_valid = false; } } }
return is_valid; }
CIPHER.H
#ifndef CIPHER_H_ #define CIPHER_H_ #include
using namespace std;
const unsigned int ALPHABET_SIZE = 26;
/* A class that implements a basic substitution cipher */ class Cipher { protected: struct CipherCheshire; CipherCheshire *smile; public: Cipher(); Cipher(string cipher_alpha); ~Cipher(); virtual string encrypt(string raw); virtual string decrypt(string enc); };
/* Helper function headers */ unsigned int find_pos(string alpha, char c); inline char UPPER_CASE(char c) { return toupper(c); } inline char LOWER_CASE(char c) { return tolower(c); } #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started