Added functionality for the Atbash class

This commit is contained in:
2018-04-30 12:20:55 -04:00
parent f26404f56a
commit 2ad7b479a5
2 changed files with 22 additions and 274 deletions

272
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-29-18 //Modified: 4-30-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
@@ -10,6 +10,7 @@
#include "Headers/Caesar.hpp" #include "Headers/Caesar.hpp"
#include "Headers/Playfair.hpp" #include "Headers/Playfair.hpp"
#include "Headers/Vigenere.hpp" #include "Headers/Vigenere.hpp"
#include "Headers/Atbash.hpp"
#include <iostream> #include <iostream>
#include <string> #include <string>
@@ -17,276 +18,13 @@
///Test definitions ///Test definitions
//#define TEST_VERSION //#define TEST_VERSION
#ifdef TEST_VERSION
#define CAESAR_TEST
#define PLAYFAIR_TEST
#define VIGENERE_TEST
enum codingState { ENCODE, DECODE };
std::string testingError(codingState type, const std::string& input, const std::string& output, const std::string& cipher);
#endif //TEST_VERSION definition
#ifdef CAESAR_TEST
bool caesarTest(std::string& errorString);
#endif //CAESAR_TEST
#ifdef PLAYFAIR_TEST
bool playfairTest(std::string& errorString);
#endif //PLAYFAIR_TEST
#ifdef VIGENERE_TEST
bool vigenereTest(std::string& errorString);
#endif //VIGENERE_TEST
//Main functions //Main functions
#ifdef TEST_VERSION #ifdef TEST_VERSION
int main(int argc, char** argv){ #include "testMain.hpp" //This inserts the main function with all of the test functions
bool testResult = false;
std::string resultString = "";
std::string errorString = "";
//Testing the Caesar Cipher
#ifdef CAESAR_TEST
testResult = caesarTest(errorString);
//If the test was successful
if(testResult){
resultString += "Caesar Cipher completed successfully\n";
}
else{
resultString += "Caesar Cipher error:\n" + errorString;
}
std::cout << "Test1: " << resultString << std::endl;
//Reset the variables
errorString = "";
testResult = false;
#endif //CAESAR_TEST
//Testing the playfair cipher
#ifdef PLAYFAIR_TEST
testResult = playfairTest(errorString);
if(testResult){
resultString += "Playfair Cipher completed successfully\n";
}
else{
resultString += "Playfair Cipher error: " + errorString;
}
errorString = "";
testResult = false;
#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::cin.get();
return 0;
}
std::string testingError(codingState type, const std::string& input, const std::string& output, const std::string& cipher){
std::string errorMessage;
//Decide if it was encoding or decoding
if(type == ENCODE){
errorMessage = "Encoding:";
}
else if(type == DECODE){
errorMessage = "Decoding:";
}
else{
errorMessage = "We gots problems";
}
errorMessage += "\nError in: " + input + "\nExpected: " + output + "\nActual: " + cipher + '\n';
return errorMessage;
}
#ifdef CAESAR_TEST
bool caesarTest(std::string& errorString){
///Add something in error message about shift amount
Caesar cipher;
bool passed = true;
std::string inputString = ""; //Holds the string to be input into the cipher
std::string outputString = ""; //Holds the string that the cipher is expected to output
std::string cipherString = ""; //Holds the string that the cipher actually output
///All lowercase
inputString = "abcdefghijklmnopqrstuvwxyz"; //Shift = 3
outputString = "defghijklmnopqrstuvwxyzabc"; //Shift = 3
//Try it forwards
cipherString = cipher.encode(3, inputString);
if(cipherString != outputString){
//errorString += "Encoding:\nError in: " + inputString + "\nExpected: " + outputString + "\nActual: " + cipherString + '\n';
errorString += testingError(ENCODE, inputString, outputString, cipherString);
passed = false;
}
//Try it backwards
cipherString = cipher.decode(3, outputString);
if(cipherString != inputString){
//errorString += "Decoding:\nError in: " + outputString + "\nExpected: " + inputString + "\nActual: " + cipherString + '\n';
errorString += testingError(DECODE, outputString, inputString, cipherString);
passed = false;
}
///All upercase
inputString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
outputString = "XYZABCDEFGHIJKLMNOPQRSTUVW";
//Try it forwards
cipherString = cipher.encode(-3, inputString);
if(cipherString != outputString){
errorString += testingError(ENCODE, inputString, outputString, cipherString);
passed = false;
}
//Try it backwards
cipherString = cipher.decode(-3, outputString);
if(cipherString != inputString){
errorString += testingError(DECODE, outputString, inputString, cipherString);
passed = false;
}
///Both cases
inputString = "AbCdEfGhIjKlMnOpQrStUvWxYz";
outputString = "DeFgHiJkLmNoPqRsTuVwXyZaBc";
//Try it forwards
cipherString = cipher.encode(3, inputString);
if(cipherString != outputString){
errorString += testingError(ENCODE, inputString, outputString, cipherString);
passed = false;
}
//Try it backwards
cipherString = cipher.decode(3, outputString);
if(cipherString != inputString){
errorString += testingError(DECODE, outputString, inputString, cipherString);
passed = false;
}
///Punctuation
inputString = "abc,def. rst; wxyz";
outputString = "def,ghi. uvw; zabc";
//Try it forwards
cipherString = cipher.encode(3, inputString);
if(cipherString != outputString){
errorString += testingError(ENCODE, inputString, outputString, cipherString);
passed = false;
}
//Try it backwards
cipherString = cipher.decode(3, outputString);
if(cipherString != inputString){
errorString += testingError(DECODE, outputString, inputString, cipherString);
passed = false;
}
return passed;
}
#endif //CAESAR_TEST
#ifdef PLAYFAIR_TEST
bool playfairTest(std::string& errorString){
bool passed = true;
std::string keyword, inputString, outputString, cipherString;
Playfair cipher;
//Test from wikipedia
keyword = "Playfair Example";
inputString = "Hide the gold in the tree stump";
outputString = "BMODZBXDNABEKUDMUIXMMOUVIF";
//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 //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 "helperFunctions.hpp"
#include <iostream>
#include <string>
#include <fstream> #include <fstream>
@@ -328,6 +66,9 @@ int main(int argc, char** argv){
} }
//Run the appropriate functions for the cipher //Run the appropriate functions for the cipher
Fn cipherFunction = getCipher(cipherFlags);
cipherString = cipherFunction(inputFile, cipherFlags[ENCODE]);
/*
if(cipherFlags[CAESAR]){ if(cipherFlags[CAESAR]){
cipherString = runCaesar(inputFile, cipherFlags[ENCODE]); cipherString = runCaesar(inputFile, cipherFlags[ENCODE]);
} }
@@ -341,6 +82,7 @@ int main(int argc, char** argv){
cipherString = "There is a big problem. No valid cipher option was given.\n"; cipherString = "There is a big problem. No valid cipher option was given.\n";
return 0; return 0;
} }
*/
if(failFlag){ if(failFlag){
std::cout << cipherString << std::endl; std::cout << cipherString << std::endl;

View File

@@ -1,11 +1,11 @@
#For Linux #For Linux
LinuxAll: libCaesar.a libPlayfair.a libVigenere.a Ciphers LinuxAll: libCaesar.a libPlayfair.a libVigenere.a libAtbash.a Ciphers
LinuxTest: libCaesar.a libPlayfair.a libVigenere.a CiphersTest LinuxTest: libCaesar.a libPlayfair.a libVigenere.a libAtbash.a CiphersTest
LinuxDebug: CiphersDBG LinuxDebug: CiphersDBG
#For Windows #For Windows
WindowsAll: libCaesar.lib libPlayfair.lib libVigenere.lib Ciphers.exe WindowsAll: libCaesar.lib libPlayfair.lib libVigenere.lib libAtbash.lib Ciphers.exe
WindowsTest: libCaesar.lib libPlayfair.lib libVigenere.lib CiphersTest.exe WindowsTest: libCaesar.lib libPlayfair.lib libVigenere.lib libAtbash.lib CiphersTest.exe
WindowsDebug: CipherDBG.exe WindowsDebug: CipherDBG.exe
@@ -22,11 +22,14 @@ libPlayfair.a: SourceFiles/Playfair.cpp directory
libVigenere.a: SourceFiles/Vigenere.cpp directory libVigenere.a: SourceFiles/Vigenere.cpp directory
$(CXX) -shared -std=c++11 -O3 -fPIC -o lib/$@ SourceFiles/Vigenere.cpp $(CXX) -shared -std=c++11 -O3 -fPIC -o lib/$@ SourceFiles/Vigenere.cpp
libAtbash.a: SourceFiles/Atbash.cpp directory
$(CXX) -shared -std=c++11 -O3 -fPIC -o lib/$@ SourceFiles/Atbash.cpp
Ciphers: main.cpp helperFunctions.hpp Ciphers: main.cpp helperFunctions.hpp
$(CXX) -O3 -std=c++11 -o $@ main.cpp -L ./lib -lCaesar -lPlayfair -lVigenere $(CXX) -O3 -std=c++11 -o $@ main.cpp -L ./lib -lCaesar -lPlayfair -lVigenere -lAtbash
CiphersTest: main.cpp CiphersTest: main.cpp
$(CXX) -O3 -std=c++11 -DTEST_VERSION -o Ciphers $< -L ./lib -lCaesar -lPlayfair -lVigenere $(CXX) -O3 -std=c++11 -DTEST_VERSION -o Ciphers $< -L ./lib -lCaesar -lPlayfair -lVigenere -lAtbash
CiphersDBG: main.cpp SourceFiles/Caesar.cpp SourceFiles/Playfair.cpp CiphersDBG: main.cpp SourceFiles/Caesar.cpp SourceFiles/Playfair.cpp
$(CXX) -O3 -std=c++11 -g -DTEST_VERSION -o $@ $< $(CXX) -O3 -std=c++11 -g -DTEST_VERSION -o $@ $<
@@ -41,13 +44,16 @@ libPlayfair.lib: SourceFiles/Playfair.cpp directory
g++ -shared -std=c++11 -O3 -fPIC -o lib/$@ SourceFiles/Playfair.cpp g++ -shared -std=c++11 -O3 -fPIC -o lib/$@ SourceFiles/Playfair.cpp
libVigenere.lib: SourceFiles/Vigenere.cpp directory libVigenere.lib: SourceFiles/Vigenere.cpp directory
$(CXX) -shared -std=c++11 -O3 -fPIC -o lib/$@ SourceFiles/Vigenere.cpp g++ -shared -std=c++11 -O3 -fPIC -o lib/$@ SourceFiles/Vigenere.cpp
libAtbash.lib: SourceFiles/Atbash.cpp directory
g++ -shared -std=c++11 -O3 -fPIC -o lib/$@ SourceFiles/Vigenere.cpp
Ciphers.exe: main.cpp helperFunctions.hpp Ciphers.exe: main.cpp helperFunctions.hpp
g++ -std=c++11 -O3 -o $@ main.cpp -L ./lib -llibCaesar -llibPlayfair -llibVigenere g++ -std=c++11 -O3 -o $@ main.cpp -L ./lib -llibCaesar -llibPlayfair -llibVigenere -llibAtbash
CiphersTest.exe: main.cpp CiphersTest.exe: main.cpp
g++ -std=c++11 -O3 -DTEST_VERSION -o Ciphers.exe $< -L ./lib -llibCaesar -llibPlayfair -llibVigenere g++ -std=c++11 -O3 -DTEST_VERSION -o Ciphers.exe $< -L ./lib -llibCaesar -llibPlayfair -llibVigenere -llibAtbash
CiphersDBG.exe: main.cpp SourceFiles/Caesar.cpp SourceFiles/Playfair.cpp SourceFiles/Vigenere.cpp CiphersDBG.exe: main.cpp SourceFiles/Caesar.cpp SourceFiles/Playfair.cpp SourceFiles/Vigenere.cpp
g++ -std=c++11 -O3 -g -DTEST_VERSION -o $@ main.cpp SourceFiles/Caesar.cpp SourceFiles/Playfair.cpp SourceFiles/Vigenere.cpp g++ -std=c++11 -O3 -g -DTEST_VERSION -o $@ main.cpp SourceFiles/Caesar.cpp SourceFiles/Playfair.cpp SourceFiles/Vigenere.cpp