Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming: Pointers Assignment Instructions Overview The objective of this assignment is to demonstrate the use of pointers in a program utilizing c-strings and tokenization.

C++ Programming: Pointers Assignment Instructions

Overview

The objective of this assignment is to demonstrate the use of pointers in a program utilizing c-strings and tokenization. The use of pointers is foundational in C++. This program provides an exercise in using pointers, passing them into functions, using them as return data types, and leveraging them in traversing arrays.

Instructions

Since many of you are studying the field of Cyber Security, you may find this assignment to be especially interesting as it uses a cipher that has been around for many years.

Your job is to write a program to encrypt blocks of text that can be sent over unsecured channels and decrypt blocks of text that have been received. The encryption and decryption will be accomplished 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," an excellent 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 cipher works. In this assignment, you will be given the exact formulas to use to encrypt and decrypt the text.

Before performing any encryption/decryption activities, the first step is to read blocks of text from a file and parse the text into individual words.

The Program: Setting up classes and understanding tokenization

The first step in encrypting/decrypting blocks of text is to parse the text 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 array that will hold 1000 characters.

Read the contents of a file into this array using the getline command. To view a discussion on how to use getline with a file object, see this link:https://stackoverflow.com/questions/13035674/how-to-read-line-by-line-or-a-whole-text-file-at-once

Note: I realize that it would be easier to read the file's contents into a string variable; that way, you wouldn't have to tokenize the block of text. HOWEVER... one of the objectives 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 array to read the data from the file and tokenize it as described below to receive credit for this assignment.

Using the character array, your client should call the function strtok_s() to tokenize the character array 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 you can tweak your program.

https://msdn.microsoft.com/en-us/library/ftsafwz3.aspx

Class Construction: General Overview

There should be two classes in your program: Vigenere and MyText.

Vigenere contains the encryption key and the logic for encrypting and decrypting individual words.

MyText 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 MyText 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:

  • key - string

Functions:

  • Vigenere(string k) - one-argument constructor
  • setKey(string k) - data member setter
  • getKey() - data member getter
  • toUpperCase(string k) - function that converts the string received to upper case and then returns that string
  • encrypt(string w) - function (provided below) that encrypts the string received and then returns that string
  • decrypt(string w) - function (provided below) that decrypts the string received and then returns that string

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.

Note: Due to the way the Vigenere class is connected to the MyText class, you may find you have to go directly to the Vigenere class setKey() function to get the key into the class.

To convert a string to all upper case, loop over all the characters in the string and use the toupper function (http://www.cplusplus.com/reference/cctype/toupper/ ) 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 pointers, to tokenize character arrays 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. Cite your source.

stringVigenere::encrypt(stringword)

{

string output;

for (int i = 0, j = 0; i

char c =word[i];

if (c >='a' && c

c +='A' -'a';

elseif (c 'Z')

continue;

output+= (c + key[j] - 2 *'A') % 26 +'A';//added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ]

j = (j + 1) % key.length();

}

return output;

}

stringVigenere::decrypt(stringword)

{

string output;

for (int i = 0, j = 0; i

char c =word[i];

if (c >='a' && c

c +='A' -'a';

elseif (c 'Z')

continue;

output+= (c - key[j] + 26) % 26 +'A';//added 'A' to bring 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 ]

MyText Class

Data members:

  • words - vector
  • v - Vigenere

Functions:

  • MyText(string k) - one argument constructor k is the key passed into the constructor, which is then passed into the Vigenere class's setKey function.
  • encryptWord(char* token) - function that receives one word, gets it encrypted, and then adds the word to the vector
  • decryptWord(char* token) - function that receives one word, gets it decrypted, and then adds the word to the vector
  • FileCreate(string fileName) - function that creates a file and outputs the vector of words to it.
  • DisplayText() - function that displays to the screen the list of words in the vector.
  • WordCount() - function that returns the number of words stored in the vector.

The MyText class contains 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.

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 return an integer for the selected option. After each selection, the menu should be re-displayed until the user enters "3" for Quit. The menu should look like the one in the screenshot below:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
Vigenere Cypher Main Menu 1 - Encrypt File 2 - Decrypt File 3 - Quit Selection:Enter an encryption/decryption key: MyKeyEnter the name of the file to encrypt: Pledges--Flag-Christian-Bible. txtI pledge allegiance to the Flag of the United States of America, and to the Republic for which it stands, one Nation und er God, indivisible, with liberty and justice for all. I pledge allegiance to the Christian flag, and to the Savior for whose kingdom it stands. One Savior, crucified, risen and coming again, with life and liberty for all who believe. I ple dge allegiance to the Bible, GodCOs holy Word. I will make it a lamp unto my feet and a light unto my path. I will hide its words in my heart that I might not sin against God.A new file will be created that contains the encrypted message. Please enter the name of the new file to create: Encryption. txt\f\f\fA new file will be created that contains the decrypted message. Please enter the name of the new file to create: Decryption.txt_ The new file contains the following decrypted message. I PLEDGE ALLEGIANCE TO THE FLAG OF THE UNITED STATES OF AMERICA AND TO T HE REPUBLIC FOR WHICH IT STANDS ONE NATION UNDER GOD INDIVISIBLE WITH LI BERTY AND JUSTICE FOR ALL I PLEDGE ALLEGIANCE TO THE CHRISTIAN FLAG AND TO THE SAVIOR FOR WHOSE KINGDOM IT STANDS ONE SAVIOR CRUCIFIED RISEN AND COMING AGAIN WITH LIFE AND LIBERTY FOR ALL WHO BELIEVE I PLEDGE ALLEGIA NCE TO THE BIBLE GODS HOLY WORD I WILL MAKE IT A LAMP UNTO MY FEET AND A LIGHT UNTO MY PATH I WILL HIDE ITS WORDS IN MY HEART THAT I MIGHT NOT S IN AGAINST GOD The message constains 101 words.General: Compiles and Executes correctly, without crashing Word document contains screen shots and integrity statements Appropriate internal documentation Style: No global variables File(s) is(are) closed where appropriate Code is modular Appropriate Pre-processing and using directives Member functions and variables are declared with appropriate protection (i.e. private or public) Five separate files are created for the program: Vigenere.h(header file), MyText.h (header file), Vigenere.cpp (class implementation file), MyText.cpp (class implementation file), and VigenereCypherDriver.cpp (driver file) Requirements: MyText Class Creation Appropriate data members: vector of strings (words), Vigenere object Constructor(s) as appropriate Correct member functions: encryptWord, decryptWord, DisplayText, FileCreate, WordCount()Vigenere Class Creation Appropriate data member: key Constructor(s) as appropriate Correct member functions: toUpperCase, encrypt, decrypt, setkey, getkey Inputs Prompts user to enter filename(s) with appropriate error checking Prompts user to enter encryption/decryption key main() Declares variables appropriately (uses pointers where needed for tokenization and creates a MyText object for storage and manipulation of tokens) Correctly tokenizes each word in the appropriate files using the strtok_s function Calls the encryptWord and decryptWord functions in the MyText class correctly (passing in one token at a time) Processing and Outputs Files are created and contain correct results (encrypted or decrypted text) Output to the screen is accurate and neatly presented

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions