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

C++ JUST THE DRIVER PROGRAM In the Caesar cipher, each letter is always shifted

ID: 3586835 • Letter: C

Question

C++ JUST THE DRIVER PROGRAM

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. Write decryption functions corresponding to the above.

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

Solution to given problem is provided below. Sample execution output is also provided for reference.

File: Cipher.cpp
#include <iostream>
#include <fstream>

using namespace std;

char encryptChar(char plainChar, int shift)
{
char encryptedChar = plainChar;

if (isalpha(plainChar))
{
if (islower(plainChar))
{
encryptedChar = (plainChar - 'a' + shift) % 26 + 'a';
}
else
{
encryptedChar = (plainChar - 'A' + shift) % 26 + 'A';
}
}

return encryptedChar;
}

char decryptChar(char plainChar, int shift)
{
char decryptedChar = plainChar;

if (isalpha(plainChar))
{
if (islower(plainChar))
{
decryptedChar = (plainChar - 'a' - shift + 26) % 26 + 'a';
}
else
{
decryptedChar = (plainChar - 'A' - shift + 26) % 26 + 'A';
}
}

return decryptedChar;
}

string caesarEncrypt(string plainText, int shift)
{
string encryptedText = "";

for(int i = 0; i < plainText.size(); ++i)
{
encryptedText += encryptChar(plainText[i], shift);
}

return encryptedText;
}

string caesarDecrypt(string encryptedText, int shift)
{
string decryptedText = "";
char decryptedChar;

for(int i = 0; i < encryptedText.size(); ++i)
{
decryptedText += decryptChar(encryptedText[i], shift);
}

return decryptedText;
}

string vigenereEncrypt(string plainText, string keyword)
{
string encryptedText = "";
int shift;
int keywordSize = keyword.size();

for(int i = 0; i < plainText.size(); ++i)
{
shift = tolower(keyword[i % keywordSize]) - 'a';
encryptedText += encryptChar(plainText[i], shift);
}

return encryptedText;
}

string vigenereDecrypt(string encryptedText, string keyword)
{
string decryptedText = "";
int shift;
int keywordSize = keyword.size();

for(int i = 0; i < encryptedText.size(); ++i)
{
shift = tolower(keyword[i % keywordSize]) - 'a';
decryptedText += decryptChar(encryptedText[i], shift);
}

return decryptedText;
}

int main()
{
string plainText;
string encryptedText;
string decryptedText;
char cipherType;

while (true)
{
cout << "Input string: ";
getline(cin, plainText);
if (cin.eof()) return 0;
cout << plainText << endl;

cipherType = ' ';

while (cipherType != 'c' && cipherType != 'v')
{
cout << "Choose cipher type (c/v): ";
cin >> cipherType;
if (cin.eof()) return 0;

cipherType = tolower(cipherType);
}

if (cipherType == 'c')
{
int shift;
cout << "Input shift value: ";
cin >> shift;

encryptedText = caesarEncrypt(plainText, shift);
decryptedText = caesarDecrypt(encryptedText, shift);
}
else if (cipherType == 'v')
{
string keyword;
cout << "Input keyword: ";
cin >> keyword;

encryptedText = vigenereEncrypt(plainText, keyword);
decryptedText = vigenereDecrypt(encryptedText, keyword);
}
cout << "Plain text: " << plainText << endl;
cout << "Encrypted text: " << encryptedText << endl;
cout << "Decrypted text: " << decryptedText << endl;
cin.clear();
while (cin.get() != ' ') continue;
}
}

Sample Execution Output
Input string: Hello world
Hello world
Choose cipher type (c/v): v
Input keyword: cake
Plain text: Hello world
Encrypted text: Jevpq gstln
Decrypted text: Hello world
Input string: Hello world
Hello world
Choose cipher type (c/v): c
Input shift value: 2
Plain text: Hello world
Encrypted text: Jgnnq yqtnf
Decrypted text: Hello world
Input string:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote