mirror of
https://bitbucket.org/Mattrixwv/cipherstream.git
synced 2025-12-07 02:43:56 -05:00
99 lines
2.6 KiB
C++
99 lines
2.6 KiB
C++
//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 <string>
|
|
|
|
|
|
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();
|
|
}
|