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 | |...
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 code in C#-Visual Studio Tasks The program needs to contain the following A comment header...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header containing your name and a brief description of the program Output prompting the user for the following inputs: Name as a string Length of a rectangle as a double Width of a rectangle as a double Length of a square as an int After accepting user input, the program outputs the following: User name Area of a rectangle with dimensions matching the inputs Area...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment,...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random number between 0 and the...
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket holds zero or more Products and can provide information * about the Products. One can add Products to a ShoppingBasket during its * lifetime, reset the ShoppingBasket, create a copy which contains Products of * at least a certain value, and make various queries to the ShoppingBasket. * (Thus, the number of Products that will be stored by a ShoppingBasket object * is not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT