Question

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 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 <iostream>

int main() {
  return 0;
}

rot13.cpp

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class Rot13Solutution{
public:
template <typename Iter>
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<<str;
myoutfile.close();
}
else cout << "Unable to open output file";
//cout << str << '\n';
}
myfile.close();
}
else cout << "Unable to open input file";
  
cout << "Your file data is being encrypted by ROT13 algorithm and stored in encypt.txt" << '\n';
cout << "-----------------------------------------------------------"<<'\n';
//This is for Decrypting data and store it in Decrypted file
string dline;
ifstream myEncryptedfile ("encryptFile.txt");
ofstream myDecryptedfile ("decryptFile.txt");//decrypt file is same as the orginal file i.e example.txt
if (myEncryptedfile.is_open())
{
while ( getline (myEncryptedfile,dline) )
{
int n = dline.length();

char str[n+1];
strcpy(str, dline.c_str());
sol.rot13Impl(str,str + strlen(str)) ;
if(myDecryptedfile.is_open())
{
myDecryptedfile<<str;
myDecryptedfile.close();
}
else cout << "Unable to open output decrypted(original) file";
}
myEncryptedfile.close();
}
else cout << "Unable to open file to be decrypted";
cout << "Your file data is being decrypted by ROT13 algorithm and stored in decrypt.txt" << '\n';
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;

int getCount(string key){

int i=0, len = key.length(), count=0;

while(i<len){

count += key[i];

i++;

}

count = (count%23)+3;

return count;

}

string read_file(string filename)

{

ifstream in;

in.open(filename.c_str());

string str = "", inp;

if (in.is_open())

{

while (!in.eof())

{

getline(in, inp);

str += inp + "\n";

}

return str;

}

else

{

cout << "Unable to open input file";

exit(1);

}

}

string encode(string str, int count)

{

int i, len = str.length() - 1;

string enc_str = str;

for (i = 0; i < len; i++)

{

if (str[i] >= 'A' && str[i] <= 'Z')

{

if (str[i] + count > 'Z')

enc_str[i] = 'A' + ((str[i] + count - 'Z') % 26) - 1;

else

enc_str[i] = str[i] + count;

}

else if (str[i] >= 'a' && str[i] <= 'z')

{

if (str[i] + count > 'z')

enc_str[i] = 'a' + ((str[i] + count - 'z') % 26) - 1;

else

enc_str[i] = str[i] + count;

}

}

return enc_str;

}

string decode(string str, int count)

{

int i, len = str.length() - 1;

string dec_str = str;

for (i = 0; i < len; i++)

{

if (str[i] >= 'A' && str[i] <= 'Z')

{

if (str[i] - count < 'A')

{

dec_str[i] = 'Z' - (('A' - (str[i] - count)) % 26) + 1;

}

else

dec_str[i] = str[i] - count;

}

else if (str[i] >= 'a' && str[i] <= 'z')

{

if (str[i] - count < 'a')

{

dec_str[i] = 'z' - (('a' - (str[i] - count)) % 26) + 1;

}

else

dec_str[i] = str[i] - count;

}

}

return dec_str;

}

void write_file(string str, string filename)

{

ofstream out(filename.c_str());

if(!out.is_open()){

cout << "Unable to open output file"<<endl;

exit(1);

}

out << str;

}

int main()

{

string key, str;

cout << "Enter keyword: ";

cin >> key;

int count = getCount(key);

str = read_file("decryptFile.txt");

string enc_str = encode(str, count);

write_file(enc_str, "encryptFile.txt");

cout << "Your file data is being encrypted by ROT13 algorithm and stored in encyptFile.txt" << '\n';

cout << "-----------------------------------------------------------" << '\n';

str = read_file("encryptFile.txt");

string dec_str = decode(str, count);

write_file(dec_str, "decryptFile.txt");

cout << "Unable to open file to be decrypted";

cout << "Your file data is being decrypted by ROT13 algorithm and stored in decryptFile.txt" << '\n';

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...
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
  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two...

    Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two functions: encryption and decryption. 2. Use any key you like. (Don't use deceptive in the slides) 3. choose a sentence or a paragraph you like as the plaintext. I have the code I just need the implementation in a different way // C++ code to implement Vigenere Cipher #include<bits/stdc++.h> using namespace std; // This function generates the key in // a cyclic manner until...

  • USING C LANGUAGE Write a program to compute a Caesar Cipher . You must read in...

    USING C LANGUAGE Write a program to compute a Caesar Cipher . You must read in a file of text and convert some of the letters to another letter using the following array: char key[27] = “efghijklmnopqrstuvwxyzabcd”; This means that if you encounter the letter ‘a’, then you will replace it with the letter ‘e’,etc. If you encounter any other characters in the input file (example: ‘A’ or space, etc, you will write them as is. You will write each...

  • Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user...

    Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user to specify the key and the text to be encrypted. A Caesar cipher is a simple substitution cipher wherein each letter in the message is shifted a certain number of spaces down the alphabet -- this number is called the key. a b c d e f g h i j k l m n o p q r s t u v w...

  • 12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print...

    12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print ciphertext character correctly. With offset of 3 the output should be ORIGINAL CHARACTERS A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ENCRYPTED CHARACTERS D E F G H I J K L M N O P Q R S T U V W X Y Z...

  • This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = '...

    This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = ' '; private static final char UPPER_BOUND = '_'; private static final int RANGE = UPPER_BOUND - LOWER_BOUND + 1; /** * This method determines if a string is within the allowable bounds of ASCII codes * according to the LOWER_BOUND and UPPER_BOUND characters * @param plainText a string to be encrypted, if it is within the allowable bounds * @return true if all characters...

  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

  • 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...

  • 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