From 59759f69bb32632d78567a19fd344d83bcff6eb4 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 30 Apr 2018 12:19:01 -0400 Subject: [PATCH] Created class for Atbash cipher --- Headers/Atbash.hpp | 31 ++++++++++++++++++ SourceFiles/Atbash.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 Headers/Atbash.hpp create mode 100644 SourceFiles/Atbash.cpp diff --git a/Headers/Atbash.hpp b/Headers/Atbash.hpp new file mode 100644 index 0000000..e2cade8 --- /dev/null +++ b/Headers/Atbash.hpp @@ -0,0 +1,31 @@ +//Ciphers/Headers/Atbash.hpp +//Matthew Ellison +// Created: 4-30-18 +//Modified: 4-30-18 +//This file contains the declaration of the Atbash class +//This class is used to encode and decode an Atbash cipher + +#ifndef ATBASH_HPP +#define ATBASH_HPP + +#include + + +class Atbash{ +private: + std::string inputString; + std::string outputString; +public: + Atbash(); + ~Atbash(); + void setInputString(std::string input); + std::string getInputString() const; + std::string getOutputString() const; + std::string encode(); + std::string encode(std::string input); + std::string decode(); + std::string decode(std::string input); + void reset(); +}; + +#endif //ATBASH_HPP diff --git a/SourceFiles/Atbash.cpp b/SourceFiles/Atbash.cpp new file mode 100644 index 0000000..7bb21f0 --- /dev/null +++ b/SourceFiles/Atbash.cpp @@ -0,0 +1,74 @@ +//Ciphers/SourceFiles/Atbash.cpp +//Matthew Ellison +// Created: 4-30-18 +//Modified: 4-30-18 +//This file contains the implementation of the Atbash class + + +#include "../Headers/Atbash.hpp" +#include + +Atbash::Atbash(){ +} + +Atbash::~Atbash(){ +} + +void Atbash::setInputString(std::string input){ + //Make sure inputString is empty + reset(); + //Strip all punctuation and whitespace from input and make all letters capital + for(int cnt = 0;cnt < input.size();++cnt){ + char letter = input[cnt]; + //If it is upper case add it to the inputString + if(isupper(letter)){ + inputString += letter; + } + //If it is lower case make it upper case and add it to the inputString + else if(islower(letter)){ + inputString += toupper(letter); + } + //If it is anything else ignore it + } +} + +std::string Atbash::getInputString() const{ + return inputString; +} + +std::string Atbash::getOutputString() const{ + return outputString; +} + +std::string Atbash::encode(){ + //Step through every element in the inputString and shift it the correct amount + for(int cnt = 0;cnt < inputString.size();++cnt){ + outputString += (inputString[cnt] + 25 - (2 * (inputString[cnt] - 'A'))); + } + + return outputString; +} + +std::string Atbash::encode(std::string input){ + setInputString(input); + return encode(); +} + +std::string Atbash::decode(){ + //Make sure outputString is empty + outputString = ""; + for(int cnt = 0;cnt < inputString.size();++cnt){ + outputString += (inputString[cnt] + 25 - (2 * (inputString[cnt] - 'A'))); + } + + return outputString; +} + +std::string Atbash::decode(std::string input){ + setInputString(input); + return decode(); +} + +void Atbash::reset(){ + inputString = outputString = ""; +}