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...
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 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;             }         }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT