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.
This is the question about the java problem, please give the detail comment and code of...
This is the question about the java problem, please give the detail comment and code of each class. Please write tests for all the code of all the classes Thank you Create a class Mammal with the following UML diagrams: +---------------------------------+ | Mammal | +---------------------------------+ | - name: String | +---------------------------------+ | + Mammal(String name) | | + getName(): String | | + isCookable(): boolean | | + testMammal(): void | (this method is static ) +---------------------------------+ The isCookable method...
This the question about the java and please show the detail comment and code of each...
This the question about the java and please show the detail comment and code of each class.Thank you. And the "+" means "public" the "-" means "private" and testBankAccount(): void testMobilePhone(): void testChocolate(): void all are static Create four classes with the following UML diagrams: +-----------------------------+ | BankAccount | +-----------------------------+ | - money: int | +-----------------------------+ | + BankAccount(int money) | | + getMoney(): int | | + setMoney(int money): void | | + testBankAccount(): void | +-----------------------------+ +------------------------------------------------+ |...
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 | |...
This problem is about java program and please show the detail comment and code in each...
This problem is about java program and please show the detail comment and code in each class. Thank you! Create four classes with the following UML diagrams: (The "-" means private and the testBankAccount() testMobilePhone() testChocolate() testStudent() all static +-----------------------------+ | BankAccount | +-----------------------------+ | - money: int | +-----------------------------+ | + BankAccount(int money) | | + getMoney(): int | | + setMoney(int money): void | | + testBankAccount(): void | +-----------------------------+ +------------------------------------------------+ | MobilePhone     | +------------------------------------------------+ | -...
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...
I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS ....
I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS . Add the following functions to the class arrayListType: Then, update the main function to test these new functions. removeAll - which removes ALL of the instances of a value in the list min - which returns the minimum value in the list max - which returns the maximum value in the list arrayListType.h : #ifndef H_arrayListType #define H_arrayListType class arrayListType { public: bool isEmpty()...
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT