Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I NEED ALL OF THEM. NO RECURSIVE, ONLY STRINGS. PLEASE DON\'T ANSWER THE QUESTIO

ID: 3586974 • Letter: I

Question

I NEED ALL OF THEM. NO RECURSIVE, ONLY STRINGS. PLEASE DON'T ANSWER THE QUESTION IF YOU'RE NOT SURE ABOUT IT.

THANK YOU.

TASK 1:

The Caesar cipher is a simple and widely known encryption technique. The action of a Caesar cipher is to replace each letter in the unencrypted message (called "plaintext" in cryptography) with a different one a fixed number of places down the alphabet. The resulting message is called "ciphertext". For example:

Plaintext: Hello, world!
Ciphertext: Ebiil, tloia!

The cipher illustrated above uses a left shift of three (or a right shift of 23), so that (for example) each occurrence of E in the plaintext becomes B in the ciphertext.

Write a C++ function to implement the Caesar cipher, using the prototype:

   precond: numpos>=0 and rshift<=26
     postcond: the return value is the encrypted version of orig using the caesar(rshift) cipher
     string encryptCaesar(string orig, int rshift);

Your function should preserve case, and any non-alphabetic characters should be left unchanged. For example, encryptCaesar("Hello, world!",23) = "Ebiil, tloia!".

You may write additional helper functions if you wish (and it will help you in later tasks). For example, one useful function may be to shift a single character some number of positions. Remember: Separation of Concerns!

TASK 2:

In the Caesar cipher, each letter is always shifted by the same value. What if we shifted each letter by a different value? A cipher known as the Vigenere cipher consists of several Caesar ciphers in sequence with different shift values. For example, we might shift the first letter of the plaintext to the right by five, the second by 17, etc. The sequence is defined by a keyword where each letter defines the shift value. If a letter in the keyword is the nth letter in the alphabet, then each ciphertext letter will be the corresponding plaintext letter shifted to the right by n-1. For example, suppose that the plaintext to be encrypted is:

Hello, world!

...and the person sending the message chooses the keyword "cake". The first letter of the keyword is 'c', and 'c' is the third letter of the alphabet. That means we shift the first letter of the plaintext to the right by 3-1 = 2, which makes the first letter of the ciphertext 'J'. Then repeat for the remaining letters. If we reach the end of the keyword, go back and use the first letter of the keyword. Following these steps, the resulting ciphertext is:

Jevpq, wyvnd!

Write a C++ function to implement the Vigenere cipher. You may make your own prototype though it should return the encrypted string as above. Again, your function should preserve case, and any non-alphabetic characters should be left unchanged.

TASK 3:

Write decryption functions corresponding to the above.

TASK 4:  

Write a driver program to test your code. This program should prompt/input a string, output the encrypted strings, and check that encrypting and decrypting a string results in the original string. Pseudocode for main is:
     Loop the following until the user inputs ctrl-d (EOF)
     Prompt for string
     Input string s
     Prompt for a character to indicate which type of cipher the user wants, and appropriate parameters.
     Encrypt s and output
     Decrypt the result and check that it is the same as s. Also output it.

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;

char caesar(char); // caesar ciper function

class Vigenere // vigenere ciper function
{
public:
string key;

Vigenere(string key)
{
for (int i = 0; i < key.size(); ++i)
{
if (key[i] >= 'A' && key[i] <= 'Z')
this->key += key[i];
else if (key[i] >= 'a' && key[i] <= 'z')
this->key += key[i] + 'A' - 'a';
}
}

string encrypt(string text) // encrption function for vigenere ciper
{
string out;

for (int i = 0, j = 0; i < text.length(); ++i)
{
char c = text[i];

if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if (c < 'A' || c > 'Z')
continue;

out += (c + key[j] - 2 * 'A') % 26 + 'A';
j = (j + 1) % key.length();
}

return out;
}

string decrypt(string text)    // decrption function for vigenere ciper
{
string out;

for (int i = 0, j = 0; i < text.length(); ++i)
{
char c = text[i];

if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if (c < 'A' || c > 'Z')
continue;

out += (c - key[j] + 26) % 26 + 'A';
j = (j + 1) % key.length();
}

return out;
}
};

char caesar(char c)   // function for caesar ciper
{
if (isalpha(c))
{
c = toupper(c); //use upper to keep from having to use two seperate for A..Z a..z
c = (((c - 65) + 13) % 26) + 65;
}
//if c isn't alpha, just send it back.
return c;
}

int main()
{
string input;
int ch;
do
{
cout << "Enter cipertext and press enter to continue." << endl;
cout << "Enter blank line to quit." << endl;
getline(cin, input);
string output = "";
for (int x = 0; x < input.length(); x++)
{
output += caesar(input[x]);
}
cout << output << endl;
}while (!input.length() == 0);
  
Vigenere cipher("VIGENERECIPHER");

string original =
"Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
string encrypted = cipher.encrypt(original);
string decrypted = cipher.decrypt(encrypted);

cout << original << endl;
cout << "Encrypted: " << encrypted << endl;
cout << "Decrypted: " << decrypted << endl;
}