Question

In C++ Having heard you have gotten really good at programming, your friend has come to...

In C++

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. All you need to do is implement the member functions for the basic encoder and then implement the padding encoder.

#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;

}


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!

Hints:
- Comment out anything related to the padding encoder and implement the simple encoder first
- Implement the function which encodes a single char and use that to encode a string
- Once you have completed the simple encoder, implement the padding encoder one function at a time

0 0
Add a comment Improve this question Transcribed image text
Answer #1

copyable code:

//Include the needed files
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

//Use the namespace std
using namespace std;

//Encoder class
class Encoder
{

public:

   //Constructor
   Encoder(const string& encryption_key);

   //Encode function
   string encode(const string& plainText);

protected:

   //encodeChar method
   char encodeChar(char c);

private:

   //Private variables
   string key;
   string alpha;

};

//PaddingEncoder class
class PaddingEncoder : public Encoder
{

public:

   //Constructor
   PaddingEncoder(const string& encryption_key, int padamt);

   //Encode function
   string encode(const string& plainText);

   //decode function
   string decode(const string& cipherText);

private:
   int padAmount;
   string charSet;
};

//Constructor
Encoder::Encoder(const string& encryption_key)
{
   //Set key
   key=encryption_key;

   //set alpha
   alpha="abcdefghijklmnopqrstuvwxyz";
}

//Encode function
string Encoder::encode(const string& plainText)
{

//declare retStr
   string retStr="";

   //Declare kk
   int kk;

   //For loop
   for(kk=0;kk<plainText.length();kk++)
   {

       //Get character
       char charC=plainText[kk];

       //Check charC is letter
       if((charC>='a'&& charC<='z')||(charC>='A'&& charC<='Z'))

           //Call function
           retStr += encodeChar(charC);

       //If not letter
       else

           //save as it is
           retStr += charC;
   }

   //Return
   return retStr;
}

//Define method to encode single character charC
char Encoder::encodeChar(char c)
{

   //Declare fg
   int fg=0;

   //Declare retChar
   char retChar=c;

   //Check c is uppercase
   if(c>='A' && c<='Z')
   {

       //set fg value
       fg=1;

       //COnvert to lower
       c=tolower(c);
   }

   //Loop to get position of c
   for(int kk=0;kk<26;kk++)
   {

       //If c found
       if(c==alpha[kk])

           //get the key character
           retChar=key[kk];
   }

   //If c is uppercase
   if(fg==1)

       //Convert retChar to uppercase
       retChar=toupper(retChar);

   //Return
   return retChar;


}

//Constructor
PaddingEncoder::PaddingEncoder(const string& encryption_key, int padamt):Encoder(encryption_key)
{
   //Set the padAmount
   padAmount=padamt;

   //Declare the character set
   charSet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
}

//Encode function
string PaddingEncoder::encode(const string& plainText)
{

   //Declare retStr
   string retStr="";

   //Declare kk
   int kk;

   //Loop to encrypt plainText
   for(kk=0;kk<plainText.length();kk++)
   {

       //Get character
       char charC=plainText[kk];

       //Check charC is letter
       if((charC>='a'&& charC<='z')||(charC>='A'&& charC<='Z'))
       {

           //Call Function
           retStr += encodeChar(charC);
              
       }

       //If not letter
       else

           //store as it is
           retStr += charC;

       //loop to pad padAmount characters
       for(int aa=0;aa<padAmount;aa++)
       {

           //Select random number
           int tp=rand()%62;

           //Pad random character
           retStr += charSet[tp];

       }

   }

   //Return
   return retStr;
}

//Decode function
string PaddingEncoder::decode(const string& cipherText)
{

   //Declare retStr
   string retStr="";

   //Declare kk
   int kk;

   //Loop to decrypt cipherText
   for(kk=0;kk<cipherText.length();kk=kk+padAmount+1)
   {

       //Get character
       char charC=cipherText[kk];

       //Check charC is letter
       if((charC>='a'&& charC<='z')||(charC>='A'&& charC<='Z'))
       {

           //Call function
           retStr += encodeChar(charC);
              
       }

       //If not letter
       else

           //Store as it is
           retStr += charC;
   }

   //Return
   return retStr;
}

//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");

   //Call function
   string cipher_text = encoder.encode(PLAIN_TEXT);

   //Display value
   cout<<"Encoded: "<<cipher_text<<endl;

   //Display
   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);

   //Call Function
   cipher_text = padEncoder.encode(PLAIN_TEXT);

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

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

   //Pause window
   system("pause");

   //Return
   return 0;

}

Add a comment
Know the answer?
Add Answer to:
In C++ Having heard you have gotten really good at programming, your friend has come to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having...

    cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having heard you are taking CS 55, your boss has come to ask for your help with a task. Students often come to ask her whether their GPA is good enough to transfer to some college to study some major and she has to look up the GPA requirements for a school and its majors in a spreadsheet to answer their question. She would like...

  • MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...

    MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A...

  • Requirements I have already build a hpp file for the class architecture, and your job is to imple...

    Requirements I have already build a hpp file for the class architecture, and your job is to implement the specific functions. In order to prevent name conflicts, I have already declared a namespace for payment system. There will be some more details: // username should be a combination of letters and numbers and the length should be in [6,20] // correct: ["Alice1995", "Heart2you", "love2you", "5201314"] // incorrect: ["222@_@222", "12306", "abc12?"] std::string username; // password should be a combination of letters...

  • You are to implement a MyString class which is our own limited implementation of the std::string...

    You are to implement a MyString class which is our own limited implementation of the std::string Header file and test (main) file are given in below, code for mystring.cpp. Here is header file mystring.h /* MyString class */ #ifndef MyString_H #define MyString_H #include <iostream> using namespace std; class MyString { private:    char* str;    int len; public:    MyString();    MyString(const char* s);    MyString(MyString& s);    ~MyString();    friend ostream& operator <<(ostream& os, MyString& s); // Prints string    MyString& operator=(MyString& s); //Copy assignment    MyString& operator+(MyString&...

  • In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits&gt...

    In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits> #include <iostream> #include <string> // atoi #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ const unsigned int DEFAULT_SIZE = 179; // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() {...

  • C++ / Visual Studio Implement the member function below that returns true if the given value...

    C++ / Visual Studio Implement the member function below that returns true if the given value is in the container, and false if not. Your code should use iterators so it works with any type of container and data. template< typename Value, class Container > bool member( const Value& val, const Container& cont ); just implement this function in the code below #include <iostream> #include <array> #include <deque> #include <list> #include <set> #include <string> #include <vector> using namespace std; #define...

  • == Programming Assignment == For this assignment you will write a program that reads in the...

    == Programming Assignment == For this assignment you will write a program that reads in the family data for the Martian colonies and stores that data in an accessible database. This database allows for a user to look up a family and its immediate friends. The program should store the data in a hashtable to ensure quick access time. The objective of this assignment is to learn how to implement and use a hashtable. Since this is the objective, you...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

  • C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will...

    C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will work with 2 classes to be used in a RPG videogame. The first class is the class Character. The Character class has two string type properties: name and race. The Character class also has the following methods: a constructor Character(string Name, string Race), that will set the values for the name and the race variables set/get functions for the two attributes a function print(),...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT