Question

In: Computer Science

Please do it in C++. Please comment on the code, and comments detail the run time...

Please do it in C++.

Please comment on the code, and comments detail the run time in terms of total operations and Big O complexities.

1. Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) Please derive the decoding map from the forward version.

2. Redesign the CaesarCipher class as a subclass of the SubstitutionCipher from the previous problem.

3. Design a RandomCipher class as a subclass of the SubstitutionCipher from number one so that each instance of the class relies on a random permutation of letters for its mapping.

Solutions

Expert Solution

part-1

SubstitutionCipher.h

#include <string>
#include <vector>
#include <iostream>
#include <cctype>

class SubstitutionCipher
{

public:
static constexpr int MAX = 26;
protected:
std::vector<wchar_t> encoder; // Encryption array
std::vector<wchar_t> decoder; // Decryption array

/**
* Constructor
*/
public:
SubstitutionCipher();

/**
* Sets the encoder string and generates the decoder
* @param encoderString
*/
virtual void setEncoder(const std::wstring &encoderString);

/**
* Returns String representing encrypted message.
*/
virtual std::wstring encrypt(const std::wstring &message);

/**
* Returns decrypted message given encrypted secret.
*/
virtual std::wstring decrypt(const std::wstring &secret);

/**
* Returns transformation of original String using given code.
*/
private:
std::wstring transform(const std::wstring &original, std::vector<wchar_t> &code);

public:
static void main(std::vector<std::wstring> &args);
};

SubstitutionCipher.cpp

#include "SubstitutionCipher.h"

SubstitutionCipher::SubstitutionCipher()
{
   this->encoder = std::vector<wchar_t>(MAX); // Encryption array
   this->decoder = std::vector<wchar_t>(MAX); // Decryption array
}

void SubstitutionCipher::setEncoder(const std::wstring &encoderString)
{
   this->encoder = std::vector<wchar_t>(encoderString.begin(), encoderString.end());

   // Generate decoder
   for (int i = 0; i < MAX; i++)
   {
       wchar_t ch = static_cast<wchar_t>(L'A' + i); // For each iteration ch will hold a value from A - Z
       // Find ch in the encoderString
       int pos = (int)encoderString.find(ch);
       // If pos is not found display error
       if (pos == -1)
       {
           std::wcout << L"Invalid encoder string" << std::endl;
           exit(1);
       }

       // If index is found
       // get the char at the pos in the original alphabet order
       // assign the char to decoder[i]
       this->decoder[i] = static_cast<wchar_t>(L'A' + pos);
   }
}

std::wstring SubstitutionCipher::encrypt(const std::wstring &message)
{
   return transform(message, encoder); // use encoder array
}

std::wstring SubstitutionCipher::decrypt(const std::wstring &secret)
{
   return transform(secret, decoder); // use decoder array
}

std::wstring SubstitutionCipher::transform(const std::wstring &original, std::vector<wchar_t> &code)
{
   std::vector<wchar_t> msg(original.begin(), original.end());
   for (int k = 0; k < msg.size(); k++)
   {
       if (std::isupper(msg[k]))
       { // we have a letter to change
           int j = msg[k] - L'A'; // will be value from 0 to 25
           msg[k] = code[j]; // replace the character
       }
   }
   return std::wstring(msg);
}

void SubstitutionCipher::main(std::vector<std::wstring> &args)
{

   std::wstring encoderString = std::wstring(L"SIEXHVGFUDJTRZNPCLQBKAYWOM");

   // Create SubstitutionCipher object
   SubstitutionCipher *subCipher = new SubstitutionCipher();
   // Set encoder
   subCipher->setEncoder(encoderString);

   std::wcout << L"Encryption code = " << std::wstring(subCipher->encoder) << std::endl;
   std::wcout << L"Decryption code = " << std::wstring(subCipher->decoder) << std::endl;
   std::wstring message = L"THE EAGLE IS IN PLAY; MEET AT JOE'S.";
   std::wstring coded = subCipher->encrypt(message);
   std::wcout << L"Secret: " << coded << std::endl;
   std::wstring answer = subCipher->decrypt(coded);
   std::wcout << L"Message: " << answer << std::endl;

   delete subCipher;
}

substitutioncipher_main.cpp

#include <string>
#include <vector>
#include "SubstitutionCipher.h"

int main(int argc, char **argv)
{
std::vector<std::wstring> args(argv + 1, argv + argc);
SubstitutionCipher::main(args);
}

Part 2

CaesarCipherRedesign.h

#include "SubstitutionCipher.h"
#include <string>
#include <vector>
#include <iostream>

class CaesarCipherRedesign : public SubstitutionCipher
{

/**
* Constructor that initializes the encryption and decryption arrays
*/
public:
CaesarCipherRedesign(int rotation);

static void main(std::vector<std::wstring> &args);
};

CaesarCipherRedesign.cpp

#include "CaesarCipherRedesign.h"

CaesarCipherRedesign::CaesarCipherRedesign(int rotation)
{
   // For upper case
   for (int k = 0; k < 26; k++)
   {
       encoder[k] = static_cast<wchar_t>(L'A' + (k + rotation) % SubstitutionCipher::MAX);
   }
}

void CaesarCipherRedesign::main(std::vector<std::wstring> &args)
{
   CaesarCipher *cipher = new CaesarCipher(1);
   std::wcout << L"Encryption code = " << std::wstring(cipher->encoder) << std::endl;
   std::wcout << L"Decryption code = " << std::wstring(cipher->decoder) << std::endl;
   std::wstring message = L"THE EAGLE IS IN PLAY; MEET AT JOE'S.";
   std::wstring coded = cipher->encrypt(message);
   std::wcout << L"Secret: " << coded << std::endl;
   std::wstring answer = cipher->decrypt(coded);
   std::wcout << L"Message: " << answer << std::endl; // should be plain text again

   delete cipher;
}


Related Solutions

Code the following in bash and run. Please show full code and don’t forget to comment...
Code the following in bash and run. Please show full code and don’t forget to comment your code. Make a code that takes any list of numbers and calculates and displays the mean, median and mode.
In C++ Please comment in all-new lines of code, thank you DO NOT USE ANSWERS THAT...
In C++ Please comment in all-new lines of code, thank you DO NOT USE ANSWERS THAT ALREADY BEEN POSTED, please use code from the assignment Copy-paste will be reported Write a program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120 integers...
This question is about java program. Please show the output and the detail code and comment...
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface. And the output (the test mthod )must be all the true! Thank you! Question1 Create a class Animal with the following UML specification: +-----------------------+ | Animal | +-----------------------+ | - name: String | +-----------------------+ | + Animal(String name) | | + getName(): String | | + getLegs(): int | | + canFly(): boolean | |...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
Language: C and comment code please PThreads The goal of this homework is to show an...
Language: C and comment code please PThreads The goal of this homework is to show an understanding of pthreads and C. Assignment: Write a C program to use the bitwise operations with each bitwise operation contained within its own thread. Each thread should take 1 parameter and will only modify global values. First, create global integers x and y, then, in the main function you will read in values for x and y from the user and then create the...
Assembly language: please comment on every line of code explaining each part. include head comments describing...
Assembly language: please comment on every line of code explaining each part. include head comments describing what your program does. Assignment 3A - A program that adds and subtracts 32-bit numbers After installing the assembler on the computer, enter the following program, save it, assemble it and run it. Do not forget to add a comment with your name in it. You will hand in a listing (e.g., addsum.asm) that should include your name ________________________________________ TITLE Add and Subtract (AddSum.asm)...
How do I run javscript console on Visual Studio code? Please give exact detail for thumbs...
How do I run javscript console on Visual Studio code? Please give exact detail for thumbs up. I just downloaded VS code.
Please complete the following code in C using the comments as instructions. Further instructions are below...
Please complete the following code in C using the comments as instructions. Further instructions are below the code. challenge.c // goal: print the environment variables to the file "env.txt", one per line // (If envp is NULL, the file should be empty, opening in write mode will do that.) // example: // inputs: // envp/environ = {"E1=2","E2=7",NULL} // outputs: // env.txt as a string would be "E1=2\nE2=7\n" // example: // inputs: // envp/environ = {NULL} or NULL // outputs: //...
Please Complete this C Code using the gcc compiler. Please include comments to explain each added...
Please Complete this C Code using the gcc compiler. Please include comments to explain each added line. /*This program computes the Intersection over Union of two rectangles as a percent: IoU = [Area(Intersection of R1 and R2) * 100 ] / [Area(R1) + Area(R2) - Area(Intersection of R1 and R2)] The answer will be specified as a percent: a number between 0 and 100. For example, if the rectangles do not overlap, IoU = 0%. If they are at the...
PLEASE PROVIDE COMMENTS TO BE ABLE TO UNDERSTAND CODE C++ Inventory Class Create an inventory class....
PLEASE PROVIDE COMMENTS TO BE ABLE TO UNDERSTAND CODE C++ Inventory Class Create an inventory class. The class will have the following data members, const int size = 100; int no_of_products; // the total number of products in the inventory. char name_array[size][15] ; // stores name of products double array [size][2]; // In the first column quantity of the product and in the second column the price per item of the product is stored. name_array stores the name of products,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT