Exam2 Bonus pdf O University of Houston (US) https:// uh.edu 1/courses/H. a sear
ID: 3828369 • Letter: E
Question
Exam2 Bonus pdf O University of Houston (US) https:// uh.edu 1/courses/H. a search elearning. Page: In this assignment you will practice file VO in C++ and investigate the use of strings and manipulating strings. You will also leam a bit about encryption and decryption. Requirements: You are asked to write a program in C++ that implements the Vigenere cypher. Your program will input a text file that contains encrypted text. You will decrypt the file and output the results into another file. The input file, named "Encoded-txt", is provided for you. You are asked to output your results into another text file named "Decoded.txt". The Vigenere cypher uses acode word to scramble a plain-text document into and encrypted document. The cypher can be reversed to recover the original plain-text document from the encrypted document using same code word. Generally, only alphabet characters are encrypted, and so non-letters are removed. For our purposes, we will simply ignore punctuation and spaces, and only the letters are encrypted. Upper-case letters are encrypted to upper-case and lower-case to lower-case. To encrypt a text, each letter in the text is matched with a letter in the secret code. The letters in the secret code determine the offset applied to the encrypted letter. Each code letter produces an offset, for example "a" represents an offset of 0,"b" an offset of l, "c" an offset of 2 and so on. A letter in cypher-text, say "M" is encrypted by a code letter of "a" to "M", by "b" to "N", by c to "o" and so on. The encryption that offsets a letter past "z" wraps back around to start with "a In a similar way, the secret code can decrypt an encrypted text. Each letter in the encrypted text is matched with a letter in the secret code. The code letter determines the offset applied to the encrypted letter. The code letter "a" represents an offset of 0,"b" an offset of -1, "c" an offset of -2 and so on. A letter in cypher-text, say "K" is decrypted by a code letter of "a to "K", by "b"to "J", by cto "T" and so on. Decrypted letters wrap around with "z" coming before "a Code Letter RN RN D E. EExplanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Vig
{
public:
string k;
Vig(string k)
{
for (int i = 0; i < k.size(); ++i)
{
if (k[i] >= 'A' && k[i] <= 'Z')
this->k += k[i];
else if (k[i] >= 'a' && k[i] <= 'z')
this->k += k[i] + 'A' - 'a';
}
}
string encr(string text)
{
string display;
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;
display += (c + k[j] - 2 * 'A') % 26 + 'A';
j = (j + 1) % k.length();
}
return display;
}
string decrypt(string text)
{
string display;
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;
display += (c - k[j] + 26) % 26 + 'A';
j = (j + 1) % k.length();
}
return display;
}
};
int main()
{
Vig cipher("VIGCIPHER");
string original =
"Beware";
string encrypted = cipher.encr(original);
string decrypted = cipher.decrypt(encred);
cdisplay << original << endl;
cdisplay << "Encrypted: " << encrypted << “ ”;
cdisplay << "Decrypted: " << decrypted << “ ”;
}
Displayput:
$ g++ VigCipher.cpp
$ a.display
Beware
Encrypted: WMCEEI
Decrypted: BEWARE
------------------
(program exited with code: 0)
Press return to continue
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.