in c++ Magic Decoder Ring. A one-time pad is a coding device that uses each character of a key string only once to encode and decode a corresponding character in a message string. Thus, the first character in the key is used to encode and decode the first character in the clear message, the second character in the key is used with the second character in the message, etc. No portion of the key is reused in subsequent messages, making for a very solid coding system, assuming a randomly generated key. You will need to create a class titled MagicDecoderRing that contains the variables and functions needed for obtaining the clear and key texts, encoding and decoding the messages, writing out the encoded message etc. You will need to instantiate an instance of this class in your main function to run your program and encode and decode messages. You need to further break your MagicDecoderRing code down into many small functions that each can be described with a single sentence without ‘ands” and “ors.” You are probably going to at least need functions with names such as encodeChar, decodeChar, encodeStr, decodeStr. Use notepad to develop two different files containing the clear-text original message and the key string. Using the TextFile1 class in this module’s notes, write a program that first reads in the clear.txt and key.txt files and generates a third file, encoded.txt, containing the encoded message which is also displayed to the user. All three files are closed as soon as possible after being opened. The program then reads in the key and encoded files and displays the original clear text. Be sure to use local paths so that when the program folder is moved to a different location, things still work.  Note that your MagicDecoderRing class may need to instantiate an instance of the TextFile1 class. The encoded text is created from the clear text and key string by combining the ith character in the clear text and the ith character in the key string to create the ith character in the resulting encoded message as follows: Take the ascii values of the ith character in the clear text and subtract the ith character in the key string, plus 256 (why?) mod 256 to generate a number between 0 and 255 which becomes the ith character in the encoded text. To decode the encoded text, the ith character in the key string is added to the ith character in the encoded text  mod 256 and converted back into a character to determine the ith character in the original clear text. An example might be useful. Suppose the 75th character in the clear message is "c" with an ascii value of 99 and suppose the 75th character in the key string is "#" with ASCII value 35.  (90-35+256) modules (%) 256 = 64. To decode we add 64 to 35 modulus 256 and get back 99 and our original character "c". The above encoding and decoding assumes that the key string (created from the key.txt file) is at least as long as the clear text. To insure this is the case, you should check string lengths and if needed concatenate the key string with itself a sufficient number of times until it is longer than the clear text. Note, while this does keep things working, it is bad encryption practice as the key is then used more than once, making the code easier to break. TestTextFile1.cpp   #include #include "TextFile1.h" using std::cout; using std::endl; int main() {  TextFile1 *tf1 = new TextFile1("foo.txt");  tf1->fileString = "this is a test. This is only a test.";  tf1->writeTextFileString();     delete tf1;  TextFile1 *tf = new TextFile1("frost.txt");  int x = tf->getTextFileString();  cout << "return code: " << x << endl;  cout << tf->fileString << endl;  delete tf; } TextFile1.h #ifndef TextFile1_H #define TextFile1_H #include #include #include class TextFile1 { public:         std::string fileName;         std::string fileString;         // one arg constructor         TextFile1(std::string fileName1) {                 fileName = fileName1;                 fileString = "";         }         // This class has no default constructor         TextFile1() = delete;         // reads textfile into filestring instance variable         int getTextFileString();         // write the current file string to a file         int writeTextFileString(); }; #endif TextFile1.cpp #include #include #include #include "TextFile1.h" int TextFile1::getTextFileString() {         std::ifstream input1(fileName);         if (input1.fail()) {                 std::cout << "failed to open " << fileName << std::endl;                 fileString = "";                 return -1;         }         while (!input1.eof())                 fileString += input1.get();         input1.close();         return 0;

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter9: Completing The Basics
Section9.3: The String Class
Problem 7E
icon
Related questions
Question

in c++

Magic Decoder Ring. A one-time pad is a coding device that uses each character of a key string only once to encode and decode a corresponding character in a message string. Thus, the first character in the key is used to encode and decode the first character in the clear message, the second character in the key is used with the second character in the message, etc. No portion of the key is reused in subsequent messages, making for a very solid coding system, assuming a randomly generated key.

You will need to create a class titled MagicDecoderRing that contains the variables and functions needed for obtaining the clear and key texts, encoding and decoding the messages, writing out the encoded message etc. You will need to instantiate an instance of this class in your main function to run your program and encode and decode messages. You need to further break your MagicDecoderRing code down into many small functions that each can be described with a single sentence without ‘ands” and “ors.” You are probably going to at least need functions with names such as encodeChar, decodeChar, encodeStr, decodeStr.

Use notepad to develop two different files containing the clear-text original message and the key string. Using the TextFile1 class in this module’s notes, write a program that first reads in the clear.txt and key.txt files and generates a third file, encoded.txt, containing the encoded message which is also displayed to the user. All three files are closed as soon as possible after being opened. The program then reads in the key and encoded files and displays the original clear text. Be sure to use local paths so that when the program folder is moved to a different location, things still work.  Note that your MagicDecoderRing class may need to instantiate an instance of the TextFile1 class.

The encoded text is created from the clear text and key string by combining the ith character in the clear text and the ith character in the key string to create the ith character in the resulting encoded message as follows: Take the ascii values of the ith character in the clear text and subtract the ith character in the key string, plus 256 (why?) mod 256 to generate a number between 0 and 255 which becomes the ith character in the encoded text. To decode the encoded text, the ith character in the key string is added to the ith character in the encoded text  mod 256 and converted back into a character to determine the ith character in the original clear text.

An example might be useful. Suppose the 75th character in the clear message is "c" with an ascii value of 99 and suppose the 75th character in the key string is "#" with ASCII value 35.  (90-35+256) modules (%) 256 = 64. To decode we add 64 to 35 modulus 256 and get back 99 and our original character "c".

The above encoding and decoding assumes that the key string (created from the key.txt file) is at least as long as the clear text. To insure this is the case, you should check string lengths and if needed concatenate the key string with itself a sufficient number of times until it is longer than the clear text. Note, while this does keep things working, it is bad encryption practice as the key is then used more than once, making the code easier to break.

TestTextFile1.cpp

 

#include <iostream>
#include "TextFile1.h"


using std::cout;
using std::endl;


int main() {
 TextFile1 *tf1 = new TextFile1("foo.txt");
 tf1->fileString = "this is a test. This is only a test.";
 tf1->writeTextFileString();
    delete tf1;


 TextFile1 *tf = new TextFile1("frost.txt");
 int x = tf->getTextFileString();
 cout << "return code: " << x << endl;
 cout << tf->fileString << endl;
 delete tf;
}

TextFile1.h

#ifndef TextFile1_H
#define TextFile1_H

#include <iostream>
#include <fstream>
#include <string>

class TextFile1 {
public:
        std::string fileName;
        std::string fileString;

        // one arg constructor
        TextFile1(std::string fileName1) {
                fileName = fileName1;
                fileString = "";
        }

        // This class has no default constructor
        TextFile1() = delete;

        // reads textfile into filestring instance variable
        int getTextFileString();
        // write the current file string to a file
        int writeTextFileString();
};

#endif

TextFile1.cpp


#include <string>
#include <fstream>
#include <iostream>
#include "TextFile1.h"


int TextFile1::getTextFileString() {
        std::ifstream input1(fileName);
        if (input1.fail()) {
                std::cout << "failed to open " << fileName << std::endl;
                fileString = "";
                return -1;
        }
        while (!input1.eof())
                fileString += input1.get();
        input1.close();
        return 0;

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Knowledge Booster
Problems on Dynamic Programming
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning