Updated to fix errors with not clearing strings during encoding/decoding

This commit is contained in:
2018-05-14 13:46:37 -04:00
parent dc85d6b5bc
commit c8c56ef159
4 changed files with 10 additions and 5 deletions

View File

@@ -14,8 +14,8 @@
//TODO: Might be able to make encoding and decoding faster for longer messages by actually using a grid of alphabets (Caesar Ciphers) //TODO: Might be able to make encoding and decoding faster for longer messages by actually using a grid of alphabets (Caesar Ciphers)
class Autokey : public Vigenere{ class Autokey : public Vigenere{
protected: protected:
void encodeSet(std::string& key, std::string input); //Special rules for setting the strings for encoding void encodeSet(std::string key, std::string input); //Special rules for setting the strings for encoding
void decodeSet(std::string& key, std::string input); //Setting the strings for decoding void decodeSet(std::string key, std::string input); //Setting the strings for decoding
std::string decode(); //Decodes the inputString std::string decode(); //Decodes the inputString
public: public:
Autokey(); Autokey();

View File

@@ -30,7 +30,7 @@ Autokey::~Autokey(){
* @param key The keyword used for the cipher * @param key The keyword used for the cipher
* @param input The string that you wish to encode * @param input The string that you wish to encode
*/ */
void Autokey::encodeSet(std::string& key, std::string input){ void Autokey::encodeSet(std::string key, std::string input){
//Remove all unneccessary elements from the key //Remove all unneccessary elements from the key
Vigenere::setKeyword(key); Vigenere::setKeyword(key);
key = Vigenere::getKeyword(); key = Vigenere::getKeyword();
@@ -57,10 +57,11 @@ void Autokey::encodeSet(std::string& key, std::string input){
* @param key The keyword used for the cipher * @param key The keyword used for the cipher
* @param input The string that you wish to decode * @param input The string that you wish to decode
*/ */
void Autokey::decodeSet(std::string& key, std::string input){ void Autokey::decodeSet(std::string key, std::string input){
//Remove all unneccessary elements from the key //Remove all unneccessary elements from the key
Vigenere::setKeyword(key); Vigenere::setKeyword(key);
//Remove all unneccessary elements from the input //Remove all unneccessary elements from the input
inputString = "";
Vigenere::setInputString(input); Vigenere::setInputString(input);
} }

View File

@@ -118,6 +118,7 @@ std::string Caesar::encode(){
* @return The encoded message * @return The encoded message
*/ */
std::string Caesar::encode(int shiftAmount, std::string input){ std::string Caesar::encode(int shiftAmount, std::string input){
reset();
setShift(shiftAmount); setShift(shiftAmount);
setInputString(input); setInputString(input);
return encode(); return encode();
@@ -167,6 +168,7 @@ std::string Caesar::decode(){
* @return The decoded message * @return The decoded message
*/ */
std::string Caesar::decode(int shiftAmount, std::string input){ std::string Caesar::decode(int shiftAmount, std::string input){
reset();
setShift(shiftAmount); setShift(shiftAmount);
setInputString(input); setInputString(input);
return decode(); return decode();

View File

@@ -123,6 +123,7 @@ std::vector<unsigned int> Vigenere::getOffsets() const{
* @return The encoded message * @return The encoded message
*/ */
std::string Vigenere::encode(){ std::string Vigenere::encode(){
outputString = "";
//Reserve the correct size for the output string to increase speed for longer messages //Reserve the correct size for the output string to increase speed for longer messages
outputString.reserve(inputString.size()); outputString.reserve(inputString.size());
@@ -167,6 +168,7 @@ std::string Vigenere::encode(std::string key, std::string input){
* @return The decoded message * @return The decoded message
*/ */
std::string Vigenere::decode(){ std::string Vigenere::decode(){
outputString = "";
//Reserve the correct size for the output string to increase speed for longer messages //Reserve the correct size for the output string to increase speed for longer messages
outputString.reserve(inputString.size()); outputString.reserve(inputString.size());