Updated for Viginere and moving flag functions to new file

This commit is contained in:
2018-04-30 00:59:06 -04:00
parent 1fbcbc2e31
commit cbab518eb7
2 changed files with 341 additions and 6 deletions

196
helperFunctions.hpp Normal file
View File

@@ -0,0 +1,196 @@
//Ciphers/helperFunctions.hpp
//Matthew Ellison
// Created: 4-29-18
//Modified: 4-29-18
//This file contains the driver function and the test functions for the Cipher program
//This program will use some simple ciphers that are no longer widely used but still fun to play with
#ifndef HELPER_FUNCTIONS_HPP
#define HELPER_FUNCTIONS_HPP
#include "Headers/Caesar.hpp"
#include "Headers/Playfair.hpp"
#include "Headers/Vigenere.hpp"
#include <iostream>
#include <fstream>
#include <string>
enum CipherFlagLocation { CAESAR, PLAYFAIR, VIGENERE, NUM_CIPHERS,
ENCODE, DECODE, INPUT_FILE, OUTPUT_FILE, SIZE };
std::string flagStrings[SIZE];
//Error handling
bool failFlag = false; //Set if something fails
//The functions that will run the ciphers
std::string runCaesar(std::ifstream& infile, bool encode);
std::string runPlayfair(std::ifstream& infile, bool encode);
std::string runVigenere(std::ifstream& infile, bool encode);
void getFlags(int argc, char** argv, bool cipherFlags[], std::string& inputFileString, std::string& outputFileString){
//Cycle through every argument
bool valid = false;
for(int cnt = 1;cnt < argc;++cnt){
//Step through every element in the flagStrings and compare the flags. If the flags do not match or you go out of bounds in argv throw an error
for(int stringcnt = 0;stringcnt < SIZE;++stringcnt){
if(argv[cnt] == flagStrings[stringcnt]){
valid = cipherFlags[stringcnt] = true;
//If the flag showed an input file get the file name if possible
if(stringcnt == INPUT_FILE){
//Make sure there is enough room in argv for the file name
if((cnt + 1) < argc){
inputFileString = argv[++cnt];
}
}
//If the flag showed an output file get the file name if possible
else if(stringcnt == OUTPUT_FILE){
//Make sure there is enough room in argv for the file name
if((cnt + 1) < argc){
outputFileString = argv[++cnt];
}
}
break;
}
}
//If the flag was not valid throw an error
if(!valid){
std::cout << argv[cnt] << " is an invalid flag" << std::endl;
return;
}
valid = false;
}
}
bool checkFlags(bool flags[]){
int counter = 0;
bool correct = false;
//Run through each flag and counter how many are true
for(int cnt = 0;cnt < NUM_CIPHERS;++cnt){
if(flags[cnt]){
++counter;
}
}
//Check if a valid number of flags were true
if(counter == 1){
correct = true;
}
//Check if either encode or decode is set, but not both
if((flags[ENCODE] && flags[DECODE]) || (!flags[ENCODE] && !flags[DECODE])){
correct = false;
}
return correct;
}
void setFlagStrings(){
flagStrings[CAESAR] = "-c";
flagStrings[PLAYFAIR] = "-p";
flagStrings[VIGENERE] = "-v";
flagStrings[ENCODE] = "-e";
flagStrings[DECODE] = "-d";
flagStrings[INPUT_FILE] = "-i";
flagStrings[OUTPUT_FILE] = "-o";
}
std::string runCaesar(std::ifstream& infile, bool encode){
std::string inputString, cipherString;
int offset;
Caesar cipher;
//Check if the input file is open
if(infile.is_open()){
infile >> offset;
infile.ignore(10000, '\n'); //Get rid of the \n that is right after the offset
std::getline(infile, inputString);
if(infile.fail()){
failFlag = true;
cipherString = "Input file has an incorrect format\n";
}
}
//Otherwise prompt for the appropriate strings
else{
std::cout << "Enter the cipher offset: ";
std::cin >> offset;
std::cout << "Enter the input string: ";
std::getline(std::cin, inputString);
}
//Run the correct cipher
if(encode){
cipherString = cipher.encode(offset, inputString);
}
else{
cipherString = cipher.decode(offset, inputString);
}
return cipherString;
}
std::string runPlayfair(std::ifstream& infile, bool encode){
std::string keyword, inputString, cipherString;
Playfair cipher;
//Check if the input file is open
if(infile.is_open()){
std::getline(infile, keyword);
std::getline(infile, inputString);
if(infile.fail()){
failFlag = true;
cipherString = "Input file has an incorrect format\n";
}
}
//Otherwise prompt for the appropriate strings
else{
std::cout << "Enter the keyword: ";
std::getline(std::cin, keyword);
std::cout << "Enter the input string: ";
std::getline(std::cin, inputString);
}
//Run the correct cipher
if(encode){
cipherString = cipher.encode(keyword, inputString);
}
else{
cipherString = cipher.decode(keyword, inputString);
}
return cipherString;
}
std::string runVigenere(std::ifstream& infile, bool encode){
std::string keyword, inputString, cipherString;
Vigenere cipher;
//Check if the input file is open
if(infile.is_open()){
std::getline(infile, keyword);
std::getline(infile, inputString);
if(infile.fail()){
failFlag = true;
cipherString = "Input file has an incorrect format\n";
}
}
//Otherwise prompt for the appropriate strings
else{
std::cout << "Enter the keyword: ";
std::getline(std::cin, keyword);
std::cout << "Enter the input string: ";
std::getline(std::cin, inputString);
}
//Run the correct cipher
if(encode){
cipherString = cipher.encode(keyword, inputString);
}
else{
cipherString = cipher.decode(keyword, inputString);
}
return cipherString;
}
#endif //HELPER_FUNCTIONS_HPP

149
main.cpp
View File

@@ -1,7 +1,7 @@
//Ciphers/main.cpp //Ciphers/main.cpp
//Matthew Ellison //Matthew Ellison
// Created: 4-25-18 // Created: 4-25-18
//Modified: 4-25-18 //Modified: 4-29-18
//This file contains the driver function and the test functions for the Cipher program //This file contains the driver function and the test functions for the Cipher program
//This program will use some simple ciphers that are no longer widely used but still fun to play with //This program will use some simple ciphers that are no longer widely used but still fun to play with
@@ -9,6 +9,7 @@
///The header files ///The header files
#include "Headers/Caesar.hpp" #include "Headers/Caesar.hpp"
#include "Headers/Playfair.hpp" #include "Headers/Playfair.hpp"
#include "Headers/Vigenere.hpp"
#include <iostream> #include <iostream>
#include <string> #include <string>
@@ -19,6 +20,7 @@
#ifdef TEST_VERSION #ifdef TEST_VERSION
#define CAESAR_TEST #define CAESAR_TEST
#define PLAYFAIR_TEST #define PLAYFAIR_TEST
#define VIGENERE_TEST
enum codingState { ENCODE, DECODE }; enum codingState { ENCODE, DECODE };
std::string testingError(codingState type, const std::string& input, const std::string& output, const std::string& cipher); std::string testingError(codingState type, const std::string& input, const std::string& output, const std::string& cipher);
#endif //TEST_VERSION definition #endif //TEST_VERSION definition
@@ -31,6 +33,9 @@ bool caesarTest(std::string& errorString);
bool playfairTest(std::string& errorString); bool playfairTest(std::string& errorString);
#endif //PLAYFAIR_TEST #endif //PLAYFAIR_TEST
#ifdef VIGENERE_TEST
bool vigenereTest(std::string& errorString);
#endif //VIGENERE_TEST
//Main functions //Main functions
@@ -69,6 +74,18 @@ int main(int argc, char** argv){
testResult = false; testResult = false;
#endif //PLAYFAIR_TEST #endif //PLAYFAIR_TEST
#ifdef VIGENERE_TEST
testResult = vigenereTest(errorString);
if(testResult){
resultString += "Vigenere Cipher completed successfully\n";
}
else{
resultString += "Vigenere Cipher error: " + errorString;
}
errorString = "";
testResult = false;
#endif //VIGENERE_TEST
std::cout << "Results:\n" << resultString << std::endl; std::cout << "Results:\n" << resultString << std::endl;
std::cin.get(); std::cin.get();
@@ -206,18 +223,140 @@ bool playfairTest(std::string& errorString){
} }
#endif //PLAYFAIR_TEST #endif //PLAYFAIR_TEST
#ifdef VIGENERE_TEST
bool vigenereTest(std::string& errorString){
bool passed = true;
std::string keyword, inputString, outputString, cipherString;
Vigenere cipher;
//Test from wikipedia
keyword = "lemon";
inputString = "Attack at dawn";
outputString = "LXFOPVEFRNHR";
//Setup the cipher
cipherString = cipher.encode(keyword, inputString);
//If the cipher didn't spit out the correct string throw an error
if(cipherString != outputString){
errorString += testingError(ENCODE, inputString, outputString, cipherString);
passed = false;
}
//This removes the spaces and turns everything capital so the reverse will work correctly
inputString = cipher.getInputString();
//Do the same thing in reverse
cipher.reset();
cipherString = cipher.decode(keyword, outputString);
//If the cipher didn't spit out the correct string throw an error
if(cipherString != inputString){
errorString += testingError(DECODE, outputString, inputString, cipherString);
passed = false;
}
//Second test from wikipedia
keyword = "ABCD";
inputString = "CRYPTOISSHORTFORCRYPTOGRAPHY";
outputString = "CSASTPKVSIQUTGQUCSASTPIUAQJB";
//Setup the cipher
cipherString = cipher.encode(keyword, inputString);
//If the cipher didn't spit out the correct string throw an error
if(cipherString != outputString){
errorString += testingError(ENCODE, inputString, outputString, cipherString);
passed = false;
}
//This removes the spaces and turns everything capital so the reverse will work correctly
inputString = cipher.getInputString();
//Do the same thing in reverse
cipher.reset();
cipherString = cipher.decode(keyword, outputString);
//If the cipher didn't spit out the correct string throw an error
if(cipherString != inputString){
errorString += testingError(DECODE, outputString, inputString, cipherString);
passed = false;
}
//Return if the cipher passed the tests
return passed;
}
#endif //VIGENERE_TEST
#else //TEST_VERSION main function #else //TEST_VERSION main function
#include "helperFunctions.hpp"
#include <iostream>
#include <string>
#include <fstream>
enum CipherFlagLocation { CAESAR, PLAYFAIR };
void getFlags(bool flags[]);
int main(int argc, char** argv){ int main(int argc, char** argv){
//Add the ability to input and output files with the appropriate strings bool cipherFlags[SIZE];
std::string inputFileName, outputFileName, cipherString;
std::ifstream inputFile;
std::ofstream outputFile;
setFlagStrings();
//Make sure the flags are all false by default
for(int cnt = 0;cnt < SIZE;++cnt){
cipherFlags[cnt] = false;
} }
void getFlags(bool flags[]){ //Check what flags were set
getFlags(argc, argv, cipherFlags, inputFileName, outputFileName);
//Make sure that only one of the ciphers was selected
if(!checkFlags(cipherFlags)){
std::cout << "The flags given were invalid" << std::endl;
return 0;
}
//Check if output file can be opened
if(cipherFlags[OUTPUT_FILE]){
outputFile.open(outputFileName);
if(outputFile.fail()){
std::cout << "The output file could not be opened" << std::endl;
return 0;
}
}
//Check if input file can be opened
if(cipherFlags[INPUT_FILE]){
inputFile.open(inputFileName);
if(inputFile.fail()){
std::cout << "The input file could not be opened" << std::endl;
return 0;
}
}
//Run the appropriate functions for the cipher
if(cipherFlags[CAESAR]){
cipherString = runCaesar(inputFile, cipherFlags[ENCODE]);
}
else if(cipherFlags[PLAYFAIR]){
cipherString = runPlayfair(inputFile, cipherFlags[ENCODE]);
}
else if(cipherFlags[VIGENERE]){
cipherString = runVigenere(inputFile, cipherFlags[ENCODE]);
}
else{
cipherString = "There is a big problem. No valid cipher option was given.\n";
return 0;
}
if(failFlag){
std::cout << cipherString << std::endl;
return 0;
}
//Decide where the cipher will be output
if(cipherFlags[OUTPUT_FILE]){
outputFile << cipherString << std::endl;
}
else{
std::cout << cipherString << std::endl;
}
inputFile.close();
outputFile.close();
return 0;
} }
#endif //TEST_VERSION #endif //TEST_VERSION