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

In C++ please have the program encipher uppercase and lowercase letters, and spa

ID: 3769655 • Letter: I

Question

In C++ please have the program encipher uppercase and lowercase letters, and spaces,

In cryptography, a cipher is an algorithm for performing encryption or decryption a series of well-?defined steps that can be followed as a procedure. Ciphers work at the level of individual letters, small groups of letters, or, in modern schemes, individual bits. The operation of a cipher usually depends on a piece of auxiliary information, called a key. The encrypting procedure is varied depending on the key, which changes the detailed operation of the algorithm. A key must be selected before using a cipher to encrypt a message, known as the plaintext. Without knowledge of the key, it should be difficult, if not nearly impossible, to decrypt the resulting ciphertext into readable plaintext.

The Caesar cipher is one of the oldest, simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it to communicate with his generals.

Figure 1. Example of Caesar cipher.

The Vigene?re cipher is a method of encrypting alphabetic text by using a series of different Caesar ciphers based on the letters of a keyword. It is a simple form of polyalphabetic substitution. This cipher is well known because while it is easy to understand and implement, it often appears to beginners to be unbreakable. To encrypt, a table of alphabets can be used, termed a Vigene?re table. It consists of the alphabet written out 26 times in different rows, each alphabet shifted cyclically to the left compared to the previous alphabet, corresponding to the 26 possible Caesar ciphers.

Figure 2. The Vigene?re table.

At different points in the encryption process, the cipher uses a different alphabet from one of the rows. The alphabet used at each point depends on a repeating keyword. For example, suppose that the plaintext to be encrypted is:

ATTACKATDAWN

The person sending the message chooses a keyword and repeats it until it matches the length of the plaintext, for example, the keyword "LEMON":

LEMONLEMONLE

Each row starts with a key letter. The remainder of the row holds the letters A to Z (in shifted order). Although there are 26 key rows shown, you will only use as many keys (different alphabets) as there are unique letters in the key string, here just 5 keys, {L, E, M, O, N}. For successive letters of the message, we are going to take successive letters of the key string, and encipher each message letter using its corresponding key row. Choose the next letter of the key, go along that row to find the column heading that matches the message character; the letter at the intersection of

[key-?row, message-?column] is the enciphered letter.

For example, the first letter of the plaintext, A, is paired with L, the first letter of the key. So use row L and column A of the Vigene?re square, namely L. Similarly, for the second letter of the plaintext, the second letter of the key is used; the letter at row E and column T is X. The rest of the plaintext is enciphered in a similar fashion:

Plaintext: ATTACKATDAWN Key: LEMONLEMONLE Ciphertext: LXFOPVEFRNHR

Decryption is performed by going to the row in the table corresponding to the key, finding the position of the ciphertext letter in this row, and then using the column's label as the plaintext. For example, in row L (from LEMON), the ciphertext L appears in column A, which is the first plaintext letter. Next we go to row E (from LEMON), locate the ciphertext X which is found in column T, thus T is the second plaintext letter.

And now, the assignment:
Part 1: Write a program that first asks for a keyword, then allows the user to enter a line of plaintext. Your program is

to both encipher the plaintext and decipher the resultant ciphertext back to plaintext; both the ciphertext and the deciphered plaintext are to be displayed to the user. Allow the user to enter multiple keyword-?plaintext combinations until the user decides to quit. This program is to be used to debug the two functions that you are required to implement:

char encipher( char key, char plain )
Uses the Vigene?re table to encipher the passed plaintext character according to the passed key character. If the passed plaintext character is not an uppercase letter, or if the passed key character is not an uppercase letter, then the character returned by this function is to be just the passed plaintext character; otherwise, the returned character is that character found at the table location of [key-?row, message-?column].

char decipher( char key, char cipher )
Uses the Vigene?re table to decipher the passed ciphertext character according to the passed key character. If the passed ciphertext character is not an uppercase letter, or if the passed key character is not an uppercase letter, then the character returned by this function is to be just the passed ciphertext character; otherwise, the returned character is that column letter corresponding to where the ciphertext character is found within the key row.

Part2:Writetheprogramcipher.cppthatwillusethecommandlinetopassthefollowingparameters:

A) First parameter: flag to indicate whether the program is to encode (-?e) or decode (-?d) the source file.

B) Second parameter: name of the source file.

C) Third parameter: name of the destination file.

The program starts by asking for the entry of a key phrase. The program then opens up the specified input file and uses the provided key phrase to either encipher or decipher the input file (based on the flag specification), with the result being written to the specified output file.

Additionally, your functions are to implement improvements to the Vigene?re table that takes into account lowercase letters, spaces, and punctuation.

Explanation / Answer

Part 1

Answer:

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

class Vigenere
{
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)
{
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)
{
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;
}
};

int main()
{
Vigenere cipher("VIGENERECIPHER");

string original;
cout << "Enter a String";
gets(original);
string encrypted = cipher.encrypt(original);
string decrypted = cipher.decrypt(encrypted);

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

Part 2

Answer:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class Vigenere
{
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)
{
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)
{
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;
}
};

int main(int argc, char *argv[])
{

Vigenere cipher("VIGENERECIPHER");
string original;

//cout << "Enter a String";
// gets(original);


ifstream myinfile(argv[1]); // opening the argument 1 file
ofstream myoutfile(argv[2]); // argument 2 i.e. the output file.
getline(myfile,original);
if(argv[0] == 'e') // argument 0 i.e. d or e
string encrypted = cipher.encrypt(original);   
else
string decrypted = cipher.decrypt(original);


  
}