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

In this assignment you’ll write a program that encrypts the alphabetic letters i

ID: 3752628 • Letter: I

Question

In this assignment you’ll write a program that encrypts the alphabetic letters in a file using the Vigènere cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below.

Please note there are 5 key files, respectively named k0.txt thru k4.txt. There are also 5 plaintext files named p0.txt thru p4.txt. And when you unpack the ZIP you'll also see the 5 base case outputs used to check your program's outputs. The base cases are the standard solution for the five respective key and plaintext files.

The test files and script are in the ZIP:pa1Files.zip Use the text at the very bottom of k2 and p2 as reference if you dont wanna use the drop box

https://www.dropbox.com/s/iwa3rv2e384hint/pa1Files.zip?dl=0

1     Vigènere Cipher

In this assignment you’ll write a program that encrypts the alphabetic letters in a file using the Vigènere cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below.

1.1 Command line parameters

Your program must compile and run from the command line.

Input the required file names as command line parameters. Your program may NOT prompt the user to enter the file names. The first parameter must be the name of the encryption key file, as described below. The second parameter must be the name of the file to be encrypted, as also described below. The sample run command near the end of this document contains an example of how the parameters will be entered.

Your program should open the two files, echo the processed input to the screen, make thenecessary calculations, and then output the ciphertext to the console (terminal) screen in the format described below.

Note

If the plaintext file to be encrypted doesn’t have the proper number (512) of alphabetic characters, pad the last block as necessary with the lowercase letter x. Make sure that all the input characters are lower case only.

1.2 Formats

Encryption Key File Formats

The encryption key is plain text that may contain upper and lower case letters, numbers, and other text. The input must be stripped of all non-alphabetic characters. Please note that the input text must be converted to contiguous lower case letters to simplify the encryption process.

Encryption Plaintext File Formats

The file to be encrypted can be any valid text file with no more than 512 letters in it. (Thus, it is safe to store all characters in the file in a character array of size 512, including any pad characters.) Please note that the input text file will also generally have punctuation, numbers, special characters, and whitespace in it, which should be ignored. You should also ignore whether a letter is uppercase or lowercase in the input file. Thus, you should treat A and a the same in your program. In order to simplify the encryption, all letters should be converted to lower case letters. In the event the plaintext input file is lessthan512 characters, pad the input file with a lowercase x until the 512 character input buffer is full.

Output Format

The program must output the following to the console (terminal) screen, also known as stdout:

Echo the lowercase alphabetic text derived from the input key file.

Echo the lowercase alphabetic text derived from the input plaintext file.

• Remember to pad with x if the processed plaintext is less than 512 characters.

Ciphertext output produced from encrypting the input key file against the input plaintextfile.

The output portion of each input file should consist of only lowercase letters in rows of exactly 80 letters per row, except for the last row, which may possibly have fewer. These characters should correspond to the ciphertext produced by encrypting all the letters in the input file. Please note that only the alphabetic letters in the input plaintext file will be encrypted. All other characters should be ignored.

1.3    Submission instructions

You must submit this assignment in Webcourses as a source file upload. Note that all submissions will be via Webcourses. The submitted programs will be tested and graded on

Eustis.

Make sure to include a comment at the top of your main source file that contains the following Academic Integrity statement (substitute your name and NID) - “I[name] ([NID])affirmthat this program is entirely my own work and that I have neither developed my code together with any another person, nor copied any code from any other person, nor permitted my code to be copied or otherwise used by any other person, nor have I copied, modified, or otherwise used programs created by others. I acknowledge that any violation of the above terms will be treated as academic dishonesty.”

1.4    Program Notes and Hints

Your program must read in an input plaintext file that may contain uppercase letters, lowercase letters and non-letter characters. Your program must distinguish between these three groups so that only the letters get encrypted. All non-letter characters in the file are simply skipped and not counted as part of the plaintext. Please note that although both upper case and lower case letters will be encrypted, your program should convert an upper case input letter to the corresponding lower case letter, i.e., it should convert an A to an a, and so on for the whole alphabet.

One possible breakdown to solve this problem is as follows:

Write a section of code or function that reads only the upper and lower case letters in theinput file into an char array of size 512, storing only the appropriate lowercase letters in the character array.

Write a section of code or function that takes as input the array from section 1 and theencryption key and produces an array of ciphertext storing only lowercase letters.

Write a section of code or function that takes as input the array storing the ciphertextand outputs it to the screen in the format specified. Additional functions or code will be needed to echo the input key and plaintext files.

1.5 Sample inputs and outputs

Sample Key File

“I think and think for months and years. Ninety-nine times, the conclusion is false. The hundredth time I am right.” - Albert Einstein “Imagination is more important than knowledge. For knowledge is limited, whereas imagination embraces the entire world, stimulating progress, giving birth to evolution.” - Albert Einstein[1]

Sample Plaintext File

“Fall in love with some activity, and do it! Nobody ever figures out what life is all about, and it doesn’t matter. Explore the world. Nearly everything is really interesting if you go into it deeply enough. Work as hard and as much as you want to on the things you like to do the best. Don’t think about what you want to be, but what you want to do. Keep up some kind of a minimum with other things so that society doesn’t stop you from doing anything at all.” - Richard Feynman[2]

Sample Ciphertext Output File

ntstvxlbyxdqgrxcdqopmpnigbyrdugvbasumqgrzxzrmynyiuchvhbsbzvnwnsldptisbnnqumwwvxa zxuafkmxlqpwpvvmlmjgkplammrrgrvxzmgpazuzwzqpzcriamxyefdvbctjbuylczxgceehhkttqpva czlzkyorwhszpatlnsfcqueezfuyefmassampvxdwervqhcxcvemwquiyshvwlvuvobuoosruvnhacoe shcknneussxfcgoaeblwndiadtbghrmrzzdjaardpfdbiyqieazczabruwglxzflagnwucgjlnkwqvml ddzwwgawaicbfyikvflamvgmegzobnrbxrepzvuaezqnqytunnqflkfpjlobfjmloqxkqqexkhkltiba dbclohkltibadbfpifjfqbatebobxpfjxdfkxqflkbjyoxzbpqebbkqfobtloiapqfjrixqfkdmoldob ppdfsfkdyfoqeqlbslirqflkxiyboqbf

[1] This is k2.txt in the Programming Assignment 1 ZIP file.

[2] This is p2.txt in the Programming Assignment 1 ZIP file.

Explanation / Answer


#include <cctype>
#include <iostream>
#include <fstream>

static const uint32_t FILE_SIZE = 512; // size of max file characters
static int keySize; // size of the key
char filteredPText[FILE_SIZE]; // variable for the array to hold filtered plaintext file
char filteredKText[FILE_SIZE]; // variable for the array to hold filtered keytext file
char encryptedText[FILE_SIZE]; // variable for the array to hold encrypted text
std::ifstream ptext; // variable for plaintext file
std::ifstream key; // variable for key file

// encrypts and prints the result
void encrypt(char *filteredKText, char *filteredPText);
void createPArray();
void createKArray();
void printArray(char *textArray, int size);

int main(int argc, char *argv[]){
   ptext.open(argv[2], std::ios::in); // opens plaintext from command line
   key.open(argv[1], std::ios::in); // opens key from command line
  
   // creates filtered arrays
   createPArray();
   createKArray();
  
   // closes the files
   ptext.close();
   key.close();
  
   // calls encrypt function on filtered arrays
   encrypt(filteredKText, filteredPText);
  
   // prints one full section of the filtered key array to the screen
   std::cout << " " << " " << "Vigenere Key:" << " " << std::endl;
   printArray(filteredKText, keySize);
   std::cout << " " << " " << std::endl;
  
   // prints the filtered plaintext array to the screen
   std::cout << "Plaintext:" << " " << std::endl;
   printArray(filteredPText, FILE_SIZE);
   std::cout << " " << " " << std::endl;
  
      
   // prints the encrypted array
   std::cout << "Ciphertext:" << " " << std::endl;
   printArray(encryptedText, FILE_SIZE);
   std::cout << std::endl;
  
   return 0;
} // end main

// creates an encrypted array from ktext and ptext file arrays
void encrypt(char *filteredKText, char *filteredPText){
   char encryptedLetter;
  
   // encrypts each letter and adds it to the encrypted array
   for(int i = 0; i < FILE_SIZE; i++){
       // adds each letter together (if the key array index is
       // greater than its boundaries it takes the modulus to
       // reset it) subtracts by 2 * 'a' to make 'a' the zero for
       // both letters, and mods by 26 so if the sum is greater
       // than 26 it will wrap back to 'a', and adds 'a' to make
       // the value a letter
       encryptedLetter = ((filteredPText[i] + (filteredKText[i % keySize]) - 2 * 'a')) % 26 + 'a';
      
       // sets the encrypted letter in the correct place in the array
       encryptedText[i] = encryptedLetter;
   } // end for
} // end encrypt

//converts plaintext file to array form
void createPArray(){
   for(int i = 0; i < FILE_SIZE; i++){
       if(!ptext.eof() && std::isalpha(ptext.peek())){
           filteredPText[i] = tolower(ptext.get());
       }
       else if(ptext.eof()){
           // pads with 'x'
           filteredPText[i] = 'x';
       }
       else{
           // if the character in the file was not alphabetic keeps
           // the array counter from incrementing and ignores the
           // current character
           i--;
           ptext.ignore();
       } // end if
   } // end for loop
} // end createPArray

// creates an array from the key file
void createKArray(){
   // reads in key until reaching end of file
   while(!key.eof()){
       // kills the while loop if the file is about to overflow
       if(keySize == 512){
           break;
       }
       // if the character is alphabetic make it lowercase and add
       // it to the filtered key array
       if(std::isalpha(key.peek())){
           filteredKText[keySize] = tolower(key.get());
           keySize ++;
       }
       // if it isn't alphabetic ignore the character
       else{
           key.ignore(1);
       } // end if
   } // end while
} // end createKArray

// prints every character in the specified array and breaks every 80
// characters
void printArray(char *textArray, int size){
   for(int i = 0; i < size; i++){
       std::cout << textArray[i];
       // if there are more than 80 characters in the line they are
       // pushed to the next line
       if((i + 1) % 80 == 0){
           std::cout << std::endl;
       } // end if
   } // end for
} // end printArray