From 1fbcbc2e31fa3fe4fd513cc3968bc09f83d95917 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 30 Apr 2018 00:58:11 -0400 Subject: [PATCH] Created the Vigenere class for Vigenere Ciphers --- Headers/Vigenere.hpp | 37 +++++++++++ SourceFiles/Vigenere.cpp | 139 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 Headers/Vigenere.hpp create mode 100644 SourceFiles/Vigenere.cpp diff --git a/Headers/Vigenere.hpp b/Headers/Vigenere.hpp new file mode 100644 index 0000000..ea4344d --- /dev/null +++ b/Headers/Vigenere.hpp @@ -0,0 +1,37 @@ +//Ciphers/SourceFiles/Vigenere.hpp +//Matthew Ellison +// Created: 4-29-18 +//Modified; 4-29-18 +//This file contains the declaration of the Vigenere class + +#ifndef VIGENERE_HPP +#define VIGENERE_HPP + + +#include +#include + +class Vigenere{ +private: + std::string inputString; //This is the string that you want to encode or decode + std::string outputString; //This is the string that is output from encoding or decoding + std::string keyword; //This is the keyword that is the resposible for determining the offsets that you change each character by + std::vector offset; //This holds the offsets computed from each character in the keyword + void setOffset(); +public: + Vigenere(); + ~Vigenere(); + void setInputString(std::string input); + std::string getInputString() const; + std::string getOutputString() const; + void setKeyword(std::string key); + std::string getKeyword() const; + std::vector getOffsets() const; + std::string encode(); + std::string encode(std::string key, std::string input); + std::string decode(); + std::string decode(std::string key, std::string input); + void reset(); +}; + +#endif //VIGENERE_HPP diff --git a/SourceFiles/Vigenere.cpp b/SourceFiles/Vigenere.cpp new file mode 100644 index 0000000..de50f56 --- /dev/null +++ b/SourceFiles/Vigenere.cpp @@ -0,0 +1,139 @@ +//Ciphers/SourceFiles/Vigenere.cpp +//Matthew Ellison +// Created: 4-29-18 +//Modified; 4-29-18 +//This file contains the implementation of the Vigenere class + +#include "../Headers/Vigenere.hpp" +#include +#include +#include + + +Vigenere::Vigenere(){ +} + +Vigenere::~Vigenere(){ +} + +void Vigenere::setInputString(std::string input){ + //Make sure inputString is clear + inputString = ""; + + //Loop through every character in input. Remove all whitespace and punctuation and make sure all letters are capital and add it to inputString + for(int cnt = 0;cnt < input.size();++cnt){ + char letter = input[cnt]; + if(isupper(letter)){ + inputString += letter; + } + else if(islower(letter)){ + inputString += toupper(letter); + } + //If the letter is whitespace or punctuation do nothing + } +} + +std::string Vigenere::getInputString() const{ + return inputString; +} + +std::string Vigenere::getOutputString() const{ + return outputString; +} + +void Vigenere::setKeyword(std::string key){ + //Make sure the keyword is blank + keyword = ""; + //Loop through every letter in the key and make sure all of them are uppercase letters + for(int cnt = 0;cnt < key.size();++cnt){ + char letter = key[cnt]; + if(isupper(letter)){ + keyword += letter; + } + else if(islower(letter)){ + keyword += toupper(letter); + } + //If it is not a letter ignore it + } + setOffset(); +} + +std::string Vigenere::getKeyword() const{ + return keyword; +} + +void Vigenere::setOffset(){ + //Make sure offset is empty + offset.clear(); + //Reserve the correct size to increase speed later + offset.reserve(keyword.size()); + + //Loop through every letter in keyword and get the offset from A + for(char letter : keyword){ + offset.push_back((letter - 'A') % 26); + } +} + +std::vector Vigenere::getOffsets() const{ + return offset; +} + +std::string Vigenere::encode(){ + //Clear the outputString + outputString = ""; + //Reserve the correct size for the output string to increase speed for longer messages + outputString.reserve(inputString.size()); + + //Step through every charater in the inputString and advance it the correct amount, according to offset + for(int cnt = 0;cnt < inputString.size();++cnt){ + char letter = (inputString[cnt] + offset[cnt % offset.size()]); //By using % you allow it to loop without having a separate counter + //Make sure the character is still a letter, if not, wrap around + if(letter < 'A'){ + letter += 26; + } + else if(letter > 'Z'){ + letter -= 26; + } + outputString += letter; + } + + return outputString; +} + +std::string Vigenere::encode(std::string key, std::string input){ + setKeyword(key); + setInputString(input); + return encode(); +} + +std::string Vigenere::decode(){ + //Clear the outputString + outputString = ""; + //Reserve the correct size for the output string to increase speed for longer messages + outputString.reserve(inputString.size()); + + //Step through every charater in the inputString and reduce it the correct amount, according to offset + for(int cnt = 0;cnt < inputString.size();++cnt){ + char letter = (inputString[cnt] - offset[cnt % offset.size()]); //By using % you allow it to loop without having a separate counter + if(letter < 'A'){ + letter += 26; + } + else if(letter > 'Z'){ + letter -= 26; + } + outputString += letter; + } + + return outputString; +} + +std::string Vigenere::decode(std::string key, std::string input){ + setKeyword(key); + setInputString(input); + return decode(); +} + +void Vigenere::reset(){ + inputString = outputString = keyword = ""; + offset.clear(); +}