write a program to encrypt outgoing messages and decrypt incoming messages using a Vigenere Cypher.
write a program to encrypt outgoing messages and decrypt incoming messages using a Vigenere Cypher. Parse a string of words from a file into tokens using the strtok_s command with pointers. In your “client” code, you will need to declare a character array that will hold 1000 characters. Using the character array, your client should call the function strtok_s() to tokenize the character array into separate words.
There should be two classes: Vigenere and Message.
Vigenere Class: Data Member: string key
Functions: Vigenere() ßconstructor
void setKey(string k)
string getKey()
string toUpperCase(string k)
string encrypt(string word)
string decrypt(string word)
The class should have a one-argument constructor. The encryption key must be in all capital letters.
Use the following code for the encrypt and decrypt functions in your Vigenere class.
string Vigenere::encrypt(string word){
string output;
for (int i = 0, j = 0; i < word.length(); ++i) {
char c = word[i];
if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if (c < 'A' || c > 'Z')
continue;
output += (c + key[j] - 2 * 'A') % 26 + 'A';
j = (j + 1) % key.length();}
return output;}
string Vigenere::decrypt(string word){
string output;
for (int i = 0, j = 0; i < word.length(); ++i) {
char c = word[i];
if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if (c < 'A' || c > 'Z')
continue;
output += (c - key[j] + 26) % 26 + 'A';
j = (j + 1) % key.length(); }
return output;}
Message Class: Data members:
Vigenere v
Functions: Message(string k)ß
void encryptWord(char* token)
void decryptWord(char* token)
void makeFile(string fileName)
void showWords()
The Message class should contain a vector of strings. The class should contain functions to encrypt a word, decrypt a word, create a file containing the vector of encrypted/decrypted words, and print.
Main()
In main(), call a function to create a menu that displays options to Encrypt a file, Decrypt a file, and Quit. The function should be called displayMenu and should return an integer for the option selected. The menu should be re-displayed after each selection until the user enters “3” for Quit.
In main(), you will need to create an object of the Message class. You will use this object to call the functions in the Message class that will encrypt, decrypt, save, and print the words retrieved from the document.
Before you create this Message object, you should prompt the user to enter an encryption/decryption key. Once the user enters the key, pass this string in to the constructor of the Message class when creating your Message object.
Menu Options:
Option 1 – Encrypt file / Option 2- Decrypt file ( both same procedure)
When the user selects this option, they should be prompted to enter the name of the file to be encrypted: This file should already exist and contain a single paragraph of information. Make sure to include appropriate error checking to ensure that the file exists and can be opened. You may test with a file that contains something like this as its contents:
Our Father, which art in heaven, Hallowed be thy Name. Thy Kingdom come. Thy will be done in earth, As it is in heaven. Give us this day our daily bread. And forgive us our trespasses, As we forgive them that trespass against us. And lead us not into temptation, But deliver us from evil. For thine is the kingdom, The power, and the glory, For ever and ever. Amen.
Your program should open the file and read its contents into a character array using the getline function. As soon as the file’s contents have been read into the character array, main() should print the contents of the array to the screen.
Next, using the strtok_s function as described above, tokenize the entire message into separate words. As each word is tokenized, call the encryptWord function in the Message class which should receive the token as a char* data type and cast it into a string. It should then pass this string into the encrypt function in the Vigenere class using the Vigenere object that is a data member in the Message class. Once the word is encrypted, the encryptWord function in the Message class should push the encrypted word onto the vector of words in the Message class.
This process is repeated until the entire file’s contents have been tokenized and encrypted.
Once the entire file has been tokenized and its encrypted words stored, main() should call the function in the Message class to create a new file that contains the encrypted words. Therefore, the user must be prompted to enter the name of the file in which the encrypted data will be stored.
The program will then create a new file based on the name that the user entered containing the contents of the words vector in the Message class. main() should call the showWords function in the Message class to print the contents of the words vector and then call the makeFile function in the Message class to create the file and print the contents of the vector to the file.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps