Question
C++ please
Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encry
2. The program will prompt the user for the following information: a. The name of encrypted file. b. The name of the cipher (
4. When you write source code, it should be readable and well-documented. Grading Criteria: 1. Rubric can be found on canvas


Hello All, I know there are still some concerns on Project 6 and I wanted to write up an explanation of what each function do
Reads in the offset and encrypted message Stores offset in the parameter reference integer named offset Close the file after
Sets the offset to zero setOffset o Input Parameters int-messageOffset o Return - None (void type) What it does: Sets the pri
3. I dont know how to get started a. Start with the file input/output functions within main. Once you can ask the user for t
A CMakeLists.txt C++ main.cpp x 5 1 #include <iostream> 2 3 int main() { 4. 01 return 0; 6 }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// ShiftCipher.h
#ifndef SHIFTCIPHER_H
#define SHIFTCIPHER_H

#include <string>
using namespace std;

class ShiftCipher
{
public:
ShiftCipher(); // default constructor
void setOffset(int messageOffset); // set the offset
string Encode(string message); // encode the input message and return the encoded message
string Decode(string message); // decode the input message and return the decoded message

private:
int offset;
};
#endif // SHIFTCIPHER_H

//end of ShiftCipher.h

// ShiftCipher.cpp
#include "ShiftCipher.h"

// default constructor to set offset to 0
ShiftCipher::ShiftCipher()
{
offset = 0;
}

// function to set the offset to the input messageOffset
void ShiftCipher::setOffset(int messageOffset)
{
offset = messageOffset;
}

// function to encode the given message using offset as the shift and return the encoded message
string ShiftCipher::Encode(string message)
{
string encodedMessage = ""; // create an empty string to store the encoded message
// since ascii code between [32,122] are allowed, hence total number of allowed characters is 91
int range = 91; // 122-32+1

// loop to encode each character in the message and append the encoded character to encodedMessage
for(size_t i=0;i<message.length();i++)
{
// to keep ascii code between [32,122]
// first subtract the ascii code of ith character from lowest code in the range, then add offset
// then to get the code between [0,90] take modulus of range and then add back 32 to get the decoded character
encodedMessage += (char)(((((message[i])-32) + offset)%range) + 32);
}

return encodedMessage;
}

// function to decode the given message using offset as the shift and return the decoded message
string ShiftCipher::Decode(string message)
{
string decodedMessage = ""; // create an empty string to store the decoded message
// since ascii code between [32,122] are allowed, hence total number of allowed characters is 91
int range = 91;// 122-32+1

// loop to decode each character in the message append the decoded character to decodedMessage
for(size_t i=0;i<message.length();i++)
{
// to keep ascii code between [32,122]
// first subtract the ascii code of ith character from lowest code in the range, then add the value return after subtracting offset from range
// then to get the code between [0,90] take modulus of range and then add back 32 to get the encoded character
decodedMessage += (char)((((message[i] - 32) + (range-offset))%range) + 32);
}

return decodedMessage;

}

//end of ShiftCipher.cpp

// main.cpp : C++ program to implement ShiftCipher class to decrypt a message read from the file using offset which is also read from the file
#include <iostream>
#include <string>
#include <fstream>
#include "ShiftCipher.h"

using namespace std;

// function declaration
void runDecryption();
string getEncryptedFileName();
string readEncryptedFile(string name, int &messageOffset);
string getDecryptedFileName();
void outputtoFile(string name, string message);


int main()
{
runDecryption();
return 0;
}

// function to run the decryption process
// Input : None
// Output : None
void runDecryption()
{
string encrypted_name, decrypted_name;
string encrypted_message, decrypted_message;
int offset;

// create a ShiftCipher object using default constructor
ShiftCipher cipher;

// get the encrypted file name from the user
encrypted_name = getEncryptedFileName();

// set the offset for the shift cipher read from the file
encrypted_message = readEncryptedFile(encrypted_name, offset);
cipher.setOffset(offset);

// decode the message using your ShiftCipher
decrypted_message = cipher.Decode(encrypted_message);

// get the output file name from the user
decrypted_name = getDecryptedFileName();

// output the message to the file
outputtoFile(decrypted_name, decrypted_message);
}

// function to get the encrypted filename from user and return it
// Input : None
// Output : string, the name of the encrypted file
string getEncryptedFileName()
{
string filename;
// input the file name from user
cout<<"What is the name of your encrypted file? ";
cin>>filename;
return filename;
}

// function to read the encrypted file to get the offset and return the encrypted message
// Inputs:
// name - string, name of the input encrypted file
// messageOffset - int, reference to return the offset read from the file
// Output: string, message read from the file
string readEncryptedFile(string name, int &messageOffset)
{
ifstream fin(name); // open the file
string message = "";

// check if file was opened
if(fin.is_open())
{
fin>>messageOffset; // read the message offset
fin.ignore(); // ignore '\n' left by cin
// read the next line of message
getline(fin, message);

fin.close(); // close the file
}

return message;
}

// function to get the decrypted filename from user and return it
// Input: None
// Output - string, filename of the decrypted file
string getDecryptedFileName()
{
string filename;
// input the name of the decrypted file
cout<<"What is the name of your decrypted file? ";
cin>>filename;
return filename;
}

// function to output the decoded message to output file
// Inputs:
// name - string, of output decrypted file
// massage - string, decrypted message to write to the file
void outputtoFile(string name, string message)
{
// output the message on screen
cout<<"message being save to "<<name<<" is:\n";
cout<<"\t"<<message<<endl;

ofstream fout(name); // open the output file

// check if file was opened successfully
if(fout.is_open())
{
fout<<message; // output the decoded message
fout.close(); // close the file
}
}

//end of main.cpp

Output:

Input files should be in the same folder as the source code files, so that it can be accessed directly using the filenames

Input file : encryptedMessage.txt

1 2 2 rtqitcookpiku cygugog

Console:

What is the name of your encrypted file? encryptedMessage.txt What is the name of your decrypted file? decryptedMessage.txt m

Output: decryptedMessage.txt

1 programming is awesome

Input file: encryptedMessage2.txt

1 5 2 nymnsp%ymnxnx&fexjsyjshj

Console:

What is the name of your encrypted file? encryptedMessage2.txt What is the name of your decrypted file? decryptedMessage2.txt

Output file : decryptedMessage2.txt

1 i think this is a sentence

Add a comment
Know the answer?
Add Answer to:
C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is my assignment prompt This is an example of the input and output This is...

    This is my assignment prompt This is an example of the input and output This is what I have so far What is the 3rd ToDo in the main.cpp for open the files and read the encrypted message? Does the rest of the code look accurate? Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a...

  • WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...

    WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...

  • Can someone please help me for this assignment? Cryptography — the science of secret writing —...

    Can someone please help me for this assignment? Cryptography — the science of secret writing — is an old science; the first recorded use was well before 1900 B.C. An Egyptian writer used previously unknown hieroglyphs in an inscription. We will use a simple substitution cypher called rot13 to encode and decode our secret messages. ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it, in...

  • MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...

    MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A...

  • Cryptography, the study of secret writing, has been around for a very long time, from simplistic...

    Cryptography, the study of secret writing, has been around for a very long time, from simplistic techniques to sophisticated mathematical techniques. No matter what the form however, there are some underlying things that must be done – encrypt the message and decrypt the encoded message. One of the earliest and simplest methods ever used to encrypt and decrypt messages is called the Caesar cipher method, used by Julius Caesar during the Gallic war. According to this method, letters of the...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program...

    Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program is required that allows a user to encode text using any of three possible ciphers. The three ciphers you are to offer are: Caesar, Playfair and Columnar Transposition. • The program needs to loop, repeating to ask the user if they wish to play with Caesar, Playfair or Columnar Transposition until the user wishes to stop the program. •For encoding, the program needs to...

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

  • Consider the following C++ program. It reads a sequence of strings from the user and uses...

    Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 +...

  • Instructions: Consider the following C++ program. It reads a sequence of strings from the user and...

    Instructions: Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT