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

Having heard you have gotten really good at programming, your friend has come to

ID: 3860296 • Letter: H

Question

Having heard you have gotten really good at programming, your friend has come to ask for your help with a simple task. She would like you to implement a program to encrypt the messages she exchanges with her friends.

The idea is very simple. Every letter of the alphabet will be substituted with some other letter according to a given key pattern like the one below.

"abcdefghijklmnopqrstuvwxyz"

"doxrhvauspntbcmqlfgwijezky" // key pattern


For example, every 'a' will become a 'd' and every 'k' will become a 'n'. Upper case letters are encoded the same way but they remain upper case (e.g. every 'A' will become a 'D'), while any characters which are not letters of the alphabet (e.g.: digits, space, punctuation, etc) remain as given (e.g. every '1' remains a '1'). The process is symmetrical so decoding of an encoded text can be done using the corresponding decoding key pattern.

Part 1: You will implement a simple encoder class which encodes strings based on a key pattern. The class has a single public function to encode a string since decoding can be done using a new encoder constructed with the symmetrical decoding key pattern.


Part 2: Your friend is pretty happy with your program but she needs an improvement. She uses a lot of common abbreviations like “LOL” when she IMs her friends and she is worried sooner or later someone will figure out the whole scheme once they notice “TMT” appearing so often in the logs.

You plan to implement a new encoder which is based on the previous one but which has the ability to pad each character with a fixed number of random letters/digits. For example, with a padding of 3, “LOL” would be encoded to a word like T###M###T### where the # are random chars from the set [a-z, A-Z, 0-9], for example TabiM8rcTyFp or ThV9MbolTJDj, each time appearing different.

The padding encoder has a new function used to decode which removes the junk characters from the encrypted texts. This way LOL can be retrieved from both TabiM8rcTyFp and ThV9MbolTJDj since LabiO8rcLyFp and LhV9ObolLJDj are not very funny :)

A starter file has been given to you here project4_first_last.cpp

/*

*

* YOUR FULL NAME HERE

*

*

* Your program report here: "I have tested this program and it has no issues" / "It has issues X/Y/Z ..."

*

*

* Output of your program for the given main here:

*

*

*

*

*

*

*

*

*

*

*/

#include <iostream>

#include <string>

#include <cstdlib>

#include <ctime>

using namespace std;

class Encoder {

public:

/*

* Create an encoder object with the given encoding key

*/

Encoder(const string& encryption_key);

/*

* Get the encrypted text for the given plain text

*/

string encode(const string& plainText);

protected:

/*

* Encode the given char

*/

char encodeChar(char c);

private:

// your code here

};

class PaddingEncoder : public Encoder {

// your code here

};

// You code here for the function implementations

/*

*

* DO NOT MODIFY THE MAIN

*

*/

int main()

{

srand(time(0));

const string PLAIN_TEXT = "C++ is fun!";

// construct an encoder and the corresponding decoder

Encoder encoder("doxrhvauspntbcmqlfgwijezky");

Encoder decoder("gmnawrseuvyqokbjpdilhftczx");

string cipher_text = encoder.encode(PLAIN_TEXT);

cout<<"Encoded: "<<cipher_text<<endl;

cout<<"Decoded: "<<decoder.encode(cipher_text)<<endl;

// construct a padded encoder with 3 character padding and the corresponding decoder

PaddingEncoder padEncoder("doxrhvauspntbcmqlfgwijezky", 3);

PaddingEncoder padDecoder("gmnawrseuvyqokbjpdilhftczx", 3);

cipher_text = padEncoder.encode(PLAIN_TEXT);

cout<<"Pad Encoded: "<<cipher_text<<endl;

cout<<"Pad Decoded: "<<padDecoder.decode(cipher_text)<<endl;

return 0;

}

All you need to do is implement the member functions for the basic encoder and then implement the padding encoder.


A sample output of the program for the string "Hello class!" is given below:

Encoded: Uhttm xtdgg!
Decoded: Hello class!
Pad Encoded: UfTohAlrty44tqiTm2OG 95Kx3zgtrNQd1GrgD5PgKQu!koU
Pad Decoded: Hello class!

Explanation / Answer

The Encryption program in c++ is as follows:

EncryptDecrypt.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int userCH ();

void encrypt ();
void decrypt ();

string XOR ( string src, int val );

int main ()
{

int ch;

while ( 1 ) {
choice = userCH ();

if ( ch == 1 ) encrypt ();
else if ( ch == 2 ) decrypt ();

else if ( ch == 3 ) break;

}

return 0;

}

int userCH ()
{
int ch;
cout << "Message encrypt" << endl;
cout << "----------------------" << endl;
cout << "1) Encrypt Message" << endl;
cout << "2) Decrypt Message" << endl;
cout << "3) Exit" << endl << endl;
cout << "Make a selection: ";
cin >> ch;
if ( ch != 1 && ch != 2 && ch != 3 ) {

cout << endl;
cout << "Please enter 1, 2, or 3" << endl;
cout << "Press enter to exit...";

cin.get ();
return 3;

}

return ch;

}
string XOR ( string src, int val )
{

for ( unsigned int loop = 0; loop < src.size (); loop ++ ) {

source [loop] ^= val;

}

return src;

}

void encrypt ()
{

char fl [255];

string mssg;
string encrypt_mssg;

int val;   
cout << endl;
cout << "Enter file name: ";
cin >> fl;

cout << "Enter secret val: ";
cin >> val;

cout << "Enter your secret mssg." << endl;
cout << "Message: ";
cin.ignore ( 255, ' ' );

getline ( cin, mssg );
encrypted_message = XOR ( mssg, val );

ofstream file ( fl );

file << encrypt_mssg;

file.close ();
cout << "Done..." << endl << endl;

}
void decrypt ()
{
char fl [255];

string mssg;

int val;

cout << endl;
cout << "Enter file name: ";
cin >> fl;

cout << "Enter secret val: ";
cin >> val;

ifstream file ( fl );

if ( !file.is_open () ) {

cout << "Failed to load " << fl << "!" << endl;
cout << "Press enter to exit...";

cin.get ();

exit ( EXIT_FAILURE );

}   
getline ( file, mssg );

file.close ();

cout << "Message: " << XOR ( mssg, val ) << endl;

cout << "Done..." << endl << endl;

}

Please rate the answer if it helped......Thankyou

Hope it helps.....

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