From 884dfb99ac2a8a77a89027e0559e4f3d140b190a Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Thu, 3 May 2018 11:37:59 -0400 Subject: [PATCH] Added new Autokey class for Autokey cipher --- Headers/Autokey.hpp | 27 ++++++++++++ SourceFiles/Autokey.cpp | 98 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 Headers/Autokey.hpp create mode 100644 SourceFiles/Autokey.cpp diff --git a/Headers/Autokey.hpp b/Headers/Autokey.hpp new file mode 100644 index 0000000..04a9012 --- /dev/null +++ b/Headers/Autokey.hpp @@ -0,0 +1,27 @@ +//Ciphers/Headers/Autokey.hpp +//Matthew Ellison +// Created: 5-2-18 +//Modified: 5-3-18 +//This file contains the declaration of the Autokey class +//This class will encode a message using the Autokey cipher + +#ifndef AUTOKEY_HPP +#define AUTOKEY_HPP + +#include "Vigenere.hpp" + + +class Autokey : public Vigenere{ +protected: + virtual void encodeSetKeyword(std::string& key, std::string input); + virtual void decodeSetKeyword(std::string& key, std::string input); + std::string decode(); +public: + Autokey(); + ~Autokey(); + virtual std::string encode(std::string key, std::string input); + virtual std::string decode(std::string key, std::string input); +}; + + +#endif //AUTOKEY_HPP diff --git a/SourceFiles/Autokey.cpp b/SourceFiles/Autokey.cpp new file mode 100644 index 0000000..093338b --- /dev/null +++ b/SourceFiles/Autokey.cpp @@ -0,0 +1,98 @@ +//Ciphers/SourceFiles/Autokey.cpp +//Matthew Ellison +// Created: 5-3-18 +//Modified: 5-3-18 +//This file contains the implementation of the Autokey class + + +#include "../Headers/Autokey.hpp" +#include + + +void Autokey::encodeSetKeyword(std::string& key, std::string input){ + //Remove all unneccessary elements from the key + Vigenere::setKeyword(key); + key = Vigenere::getKeyword(); + //Remove all unneccessary elements from the input + Vigenere::setInputString(input); + input = getInputString(); + key.append(input); + + //This will take a long time if the keyword in long + //Remove the last letter in the string until it is the same size as the input + while(key.size() > input.size()){ + key.erase(key.end() - 1); + } + + //Set the new Keyword + keyword = key; + //Make sure to update the offset + setOffset(); +} + +void Autokey::decodeSetKeyword(std::string& key, std::string input){ + //Remove all unneccessary elements from the key + Vigenere::setKeyword(key); + key = Vigenere::getKeyword(); + //Remove all unneccessary elements from the input + Vigenere::setInputString(input); +} + +std::string Autokey::decode(){ + //This is going to take a long time no matter what + //You must decode what you can and then add the decoded portion to the key and do it all over again + //std::string input; + std::string output; + std::string key = keyword; + unsigned int keyLength = keyword.size(); + for(int cnt = 0;(key.size() * cnt) < inputString.size();++cnt){ + //Make sure you are not going to run out of bounds with the keyword + //This also allows the key to be longer than the message + if(keyword.size() == keyLength){ + while(keyLength > inputString.size()){ + keyword.erase(keyword.end() - 1); + } + } + else if(keyword.size() > inputString.size()){ + while(keyword.size() > inputString.size()){ + keyword.erase(keyword.end() - 1); + } + } + else{ + while((keyword.size() + keyLength) > inputString.size()){ + //key.erase(key.end() - 1); + --keyLength; + } + } + + //Decode the portion that you have + Vigenere::decode(); + std::string temp = outputString.substr(cnt * key.size(), keyLength); + output += temp; + keyword += temp; + //Make sure to update the offsets + setOffset(); + } + + return outputString; +} + +Autokey::Autokey(){ + reset(); +} + +Autokey::~Autokey(){ +} + +std::string Autokey::encode(std::string key, std::string input){ + reset();; + encodeSetKeyword(key, input); + return Vigenere::encode(); +} + +std::string Autokey::decode(std::string key, std::string input){ + reset(); + setInputString(input); + decodeSetKeyword(key, input); //Decoding is a bit different because part of the key is also part of the original message + return Autokey::decode(); +}