Microsoft Word - CSIS112_C_Plus_Plus_Programming_Assignments_Instructions(1)
CSIS 112
Page 25 of 50
C++ Programming Assignment 5
Vigenere Cypher
Instructions:
Since many of you are studying the field of Cyber Security, you may find this assignment to be
especially interesting.
Your job is to write a program to encrypt outgoing messages and decrypt incoming messages
using a Vigenere Cypher.
In this assignment, you will parse a string of words from a file into tokens using the strtok_s
command with pointers and then use the Vigenere Cypher algorithm to encrypt and decrypt the
parsed words.
If you are unfamiliar with the “Vigenere Cypher,” a very good description of it is provided in
this short video: https:
www.youtube.com/watch?v=SkJcmCaHqS0
[Note: Don’t panic as you watch the video. I included it only to provide you with a visual
depiction of how this cypher works. In this assignment, you will be given the exact formulas to
use to encrypt and decrypt the text.]
Before you can perform any encryption/decryption activities, the first step is to read in messages
from a file and parse the messages into individual words.
The Program: Setting up classes and understanding tokenization
The first step in encrypting/decrypting messages is to parse the messages into words before
applying the encryption/decryption algorithms to them. This parsing process is called
“tokenization.”
Tokenization:
The process of parsing sentences into words is called tokenization. To tokenize a sentence into
words, use the C++ function strtok_s. [Note: do not try to use the C++ strtok function because
it has been deemed unsafe and has therefore been deprecated.]
In your “client” code (i.e. the file that contains your main function), you will need to declare a
character a
ay that will hold 1000 characters.
Read the contents of a file into this a
ay using the getline command. To view a discussion on
how to use getline with a file object, see this link:
https:
stackoverflow.com/questions/ XXXXXXXXXX/how-to-read-line-by-line-or-a-whole-text-file-at-
once
[Note: I realize that it would be easier to read the contents of the file into a string variable;
that way, you wouldn’t have to tokenize the message. HOWEVER… one of the objectives
CSIS 112
Page 26 of 50
of this assignment is to illustrate the use of tokenization of c-strings using the strtok_s
function and pointers. Therefore, you MUST use a character a
ay to read in the data
from the file and tokenize it as described below to receive credit for this assignment.]
Using the character a
ay, your client should call the function strtok_s() to tokenize the character
a
ay into separate words. Here is an excellent discussion of tokenizing and the strtok_s
function: [Note that if you scroll down the web page to the “Example” section, you will see some
code that you can tweak for your own program.]
https:
msdn.microsoft.com/en-us/li
ary/ftsafwz3.aspx
Class Construction: General Overview
There should be two classes in your program: Vigenere and Message.
Vigenere contains the encryption key and the logic for encrypting and decrypting individual
words.
Message contains a vector of words that have been encrypted or decrypted and the logic for
calling the functions in the Vigenere class to encrypt or decrypt a word. The Message class
serves as a middle-man between your client code and the Vigenere class and holds the
encrypted/decrypted results in a vector.
Class Construction: Details
Vigenere Class
Data Member: string key
Functions: Vegenere() constructor
void setKey(string k)
string getKey()
string toUpperCase(string k)
string encrypt(string word)
string decrypt(string word)
The Vigenere class should store an encryption key in a data member called “key.”
The class should have a one-argument constructor that receives a string that represents the
encryption key. The encryption key must be in all capital letters for the encryption and
decryption algorithms to work. Therefore, before setting the encryption key’s value, it should
first be converted entirely to upper case. Do this in your toUpperCase function.
To convert a string to all upper case, loop over all of the characters in the string and use the
toupper function (http:
www.cplusplus.com
eference/cctype/touppe
) to set each character in
the string equal to the uppercase version of the character.
The purpose of this assignment is not to measure your ability to create encryption and decryption
algorithms, but rather to demonstrate your ability to use the strtok_s function, along with
CSIS 112
Page 27 of 50
pointers, to tokenize character a
ays and to leverage object-oriented programming in the design
of your program. Therefore, you may 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';
added 'A' to
ing it in
ange of ASCII alphabet [ 65-90 | A-Z ]
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';
added 'A' to
ing it in range of
ASCII alphabet [ 65-90 | A-Z ]
j = (j + 1) % key.length();
}
return output;
}
[source: https:
www.tutorialspoint.com/cplusplus-program-to-implement-the-vigenere-cypher ]
Message Class
Data members: vecto
string> words
Vigenere v
Functions: Message(string k) k is the key passed in to the constructor, which is then
passed in to the Vigenere class’s setKey function.
void encryptWord(char* token)
void decryptWord(char* token)
void makeFile(string fileName)
void showWords()
The Message class should contain a vector of strings that holds the words that were either
encrypted or decrypted. The class should contain functions to encrypt a word, decrypt a word,
create a file containing the vector of encrypted or decrypted words, and print the vector of
encrypted or decrypted words.
CSIS 112
Page 28 of 50
Logic of the program
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.
The menu should look like the one in the screenshot below:
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
etrieved from the document.
Before you create this Message object, you should prompt the user to enter an
encryption/decryption key. The same key must be used for decryption as was used for
encryption. Otherwise, the algorithms will not produce the co
ect results. Once the user enters
the key, pass this string in to the constructor of the Message class when creating your Message
object. This key is ultimately converted to upper case and stored in the “key” data member in the
Vigenere class.
Menu Options:
Option 1 – Encrypt file
CSIS 112
Page 29 of 50
When the user selects this option, he or she should immediately be prompted to enter the
name of the file to be encrypted: [Note that the file name can contain spaces.]
This file should already exist and contain a single paragraph of information (not to exceed 1000
characters.) Make sure to include appropriate e
or checking to ensure that the file exists and can
e opened.
You may test with a file that contains something like this as its contents: [Capitalization
and punctuation in the file will not matter as all punctuation symbols will be stripped out in the
final result, and all letters will be converted to upper case.]
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
ead. 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 a
ay using the getline
function. As soon as the file’s contents have been read into the character a
ay, main() should
print the contents of the a
ay 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 (using
the Message object to invoke the function and passing in each token as an argument ONE AT A
TIME).
The encryptWord function in the Message class should receive the token as a char* data type
and immediately cast it into a string variable. It