Added function for Autokey

This commit is contained in:
2018-05-02 11:19:20 -04:00
parent 8970630254
commit 1c1b6d13d1

View File

@@ -1,7 +1,7 @@
//Ciphers/helperFunctions.hpp //Ciphers/helperFunctions.hpp
//Matthew Ellison //Matthew Ellison
// Created: 4-29-18 // Created: 4-29-18
//Modified: 4-29-18 //Modified: 5-2-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
@@ -16,20 +16,17 @@
#include "Headers/Vigenere.hpp" #include "Headers/Vigenere.hpp"
#include "Headers/Atbash.hpp" #include "Headers/Atbash.hpp"
#include "Headers/Morse.hpp" #include "Headers/Morse.hpp"
#include "Headers/Autokey.hpp"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <string>
enum CipherFlagLocation { CAESAR, PLAYFAIR, VIGENERE, ATBASH, MORSE, NUM_CIPHERS, enum CipherFlagLocation { CAESAR, PLAYFAIR, VIGENERE, ATBASH, MORSE, AUTOKEY, NUM_CIPHERS,
ENCODE, DECODE, INPUT_FILE, OUTPUT_FILE, SIZE }; ENCODE, DECODE, INPUT_FILE, OUTPUT_FILE, SIZE };
std::string flagStrings[SIZE]; std::string flagStrings[SIZE];
//Error handling //Error handling
bool failFlag = false; //Set if something fails 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);
//For the getCipher function //For the getCipher function
typedef std::string (*Fn)(std::ifstream&, bool); typedef std::string (*Fn)(std::ifstream&, bool);
@@ -99,6 +96,7 @@ void setFlagStrings(){
flagStrings[VIGENERE] = "-v"; flagStrings[VIGENERE] = "-v";
flagStrings[ATBASH] = "-a"; flagStrings[ATBASH] = "-a";
flagStrings[MORSE] = "-m"; flagStrings[MORSE] = "-m";
flagStrings[AUTOKEY] = "-u";
flagStrings[ENCODE] = "-e"; flagStrings[ENCODE] = "-e";
flagStrings[DECODE] = "-d"; flagStrings[DECODE] = "-d";
flagStrings[INPUT_FILE] = "-i"; flagStrings[INPUT_FILE] = "-i";
@@ -127,6 +125,10 @@ std::string runCaesar(std::ifstream& infile, bool encode){
std::getline(std::cin, inputString); std::getline(std::cin, inputString);
} }
//Check if there was an error reading the file
if(failFlag){
return cipherString;
}
//Run the correct cipher //Run the correct cipher
if(encode){ if(encode){
cipherString = cipher.encode(offset, inputString); cipherString = cipher.encode(offset, inputString);
@@ -158,6 +160,10 @@ std::string runPlayfair(std::ifstream& infile, bool encode){
std::getline(std::cin, inputString); std::getline(std::cin, inputString);
} }
//Check if there was an error reading the file
if(failFlag){
return cipherString;
}
//Run the correct cipher //Run the correct cipher
if(encode){ if(encode){
cipherString = cipher.encode(keyword, inputString); cipherString = cipher.encode(keyword, inputString);
@@ -189,6 +195,10 @@ std::string runVigenere(std::ifstream& infile, bool encode){
std::getline(std::cin, inputString); std::getline(std::cin, inputString);
} }
//Check if there was an error reading the file
if(failFlag){
return cipherString;
}
//Run the correct cipher //Run the correct cipher
if(encode){ if(encode){
cipherString = cipher.encode(keyword, inputString); cipherString = cipher.encode(keyword, inputString);
@@ -217,6 +227,10 @@ std::string runAtbash(std::ifstream& infile, bool encode){
std::getline(std::cin, inputString); std::getline(std::cin, inputString);
} }
//Check if there was an error reading the file
if(failFlag){
return cipherString;
}
//Run the correct cipher //Run the correct cipher
if(encode){ if(encode){
cipherString = cipher.encode(inputString); cipherString = cipher.encode(inputString);
@@ -245,6 +259,10 @@ std::string runMorse(std::ifstream& infile, bool encode){
std::getline(std::cin, inputString); std::getline(std::cin, inputString);
} }
//Check if there was an error reading the file
if(failFlag){
return cipherString;
}
//Run the correct cipher //Run the correct cipher
if(encode){ if(encode){
cipherString = cipher.encode(inputString); cipherString = cipher.encode(inputString);
@@ -256,6 +274,41 @@ std::string runMorse(std::ifstream& infile, bool encode){
return cipherString; return cipherString;
} }
std::string runAutokey(std::ifstream& infile, bool encode){
std::string keyword, inputString, cipherString;
Autokey 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);
}
//Check if there was an error reading the file
if(failFlag){
return cipherString;
}
//Run the correct cipher
if(encode){
cipherString = cipher.encode(keyword, inputString);
}
else{
cipherString = cipher.decode(keyword, inputString);
}
return cipherString;
}
//Returns the correct function for the flags that are set //Returns the correct function for the flags that are set
Fn getCipher(const bool cipherFlags[]){ Fn getCipher(const bool cipherFlags[]){
if(cipherFlags[CAESAR]){ if(cipherFlags[CAESAR]){