Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using C++ Part C: Implement the modified Caesar cipher Objective : The goal of part C is to create a program to encode files and

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 from the starter code. You are required to implement this as a set of at least three to four functions. It should be able to read in a specified text file, encode using a modified caesar cipher, and write it to a specified file. It should be able to decrypt it in the same way.

Using the rot13.cpp file as a template just modify the algorithm to receive a string as a key. You will use this key to calculate the rotation count. You rotate in the right direction for encryption and in the left direction for decryption.

The standard caesar cipher uses a 3 character offset for rotation. However, we are going to use an ASCII string (as a key) to determine this offset. See the following examples for details.

An example of how to calculate the key:

Given the sample key deF:

First add all the ASCII values: ASCII_SUM = d + e + F = 100 + 101 + 70 = 271

Than calculate the rotation count by using the following equation:

Count equals (ASCII_SUM % 23) + 3 = (271 % 23) + 3 = 21

The plus three in the above equation forces the code to rotate characters by at least three (3) positions.

Example:

Key: deF

Original file text:

This is a secret ring decoder that I found in a cracker jack box!

Encrypted file text:

Ocdn dn v nzxmzo mdib yzxjyzm ocvo D ajpiy di v xmvxfzm evxf wjs!

Note: The decrypted text should be the same as the original file text.

Caesar.cpp

#include  int main() { return 0; } 

rot13.cpp

#include #include #include using namespace std; class Rot13Solutution{ public: template void rot13Impl(Iter begin, const Iter& end) { while (begin != end) { char& c = *begin; if (c >= 'a' && c <= 'm') { c += 13; } else if (c >= 'n' && c <= 'z') { c -= 13; } else if (c >= 'A' && c <= 'M') { c += 13; } else if (c >= 'N' && c <= 'Z') { c -= 13; } ++begin; } } }; int main () { //This is for encrypting data and store it in encrypted file Rot13Solutution sol; string line; ifstream myfile ("decryptFile.txt"); ofstream myoutfile ("encryptFile.txt"); if (myfile.is_open()) { while ( getline (myfile,line) ) { int n = line.length();

// declaring character array char str[n+1];

// copying the contents of the string to char array strcpy(str, line.c_str()); sol.rot13Impl(str,str + strlen(str)) ; if(myoutfile.is_open()) { myoutfile<

char str[n+1]; strcpy(str, dline.c_str()); sol.rot13Impl(str,str + strlen(str)) ; if(myDecryptedfile.is_open()) { myDecryptedfile<

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions