From 909bf3278e0a4c1a7131ca089df646dd9a21a74c Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Tue, 20 Jul 2021 16:04:18 -0400 Subject: [PATCH] Update columnar-transposition input validation + Add keyWord and message validation in columnar-transposition example + Add quotes around output to help show leading / trailing spaces --- .../columnar-transposition/driver.cpp | 5 +++-- .../columnar-transposition/lib-cipher.cpp | 21 +++++++++++++++---- .../columnar-transposition/lib-cipher.hpp | 5 +++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/cpp/cryptography/columnar-transposition/driver.cpp b/cpp/cryptography/columnar-transposition/driver.cpp index fa82a67..da69af3 100644 --- a/cpp/cryptography/columnar-transposition/driver.cpp +++ b/cpp/cryptography/columnar-transposition/driver.cpp @@ -23,6 +23,7 @@ // Main program loop int main (const int argc, const char * argv[]) { // Left some test cases I used commented out :) + // + Keywords and messages can be URLs, include spaces, symbols, numbers, etc // Using example keyword from www.braingle.com // + The embedded example there doesn't seem to support numbers in keywords :( @@ -47,7 +48,7 @@ int main (const int argc, const char * argv[]) { // Take input for encrypting a message // result = cData.Encrypt(); - std::cout << "Encrypted message: " << result << std::endl; + std::cout << "Encrypted message: \"" << result << "\"\n"; exit = true; break; @@ -60,7 +61,7 @@ int main (const int argc, const char * argv[]) { // Take input for previously encrypted message to decrypt // result = cData.Decrypt(); - std::cout << "Decrypted message: " << result << std::endl; + std::cout << "Decrypted message: \"" << result << "\"\n"; exit = true; break; diff --git a/cpp/cryptography/columnar-transposition/lib-cipher.cpp b/cpp/cryptography/columnar-transposition/lib-cipher.cpp index 11450e5..7cbf9fe 100644 --- a/cpp/cryptography/columnar-transposition/lib-cipher.cpp +++ b/cpp/cryptography/columnar-transposition/lib-cipher.cpp @@ -38,7 +38,7 @@ std::string CipherData::GetKey() std::string key; std::cout << "Enter the keyword: "; std::getline(std::cin, key); - std::cout << "Keyword entered: " << key << std::endl; + std::cout << "Keyword entered: \"" << key << "\"\n"; return key; } @@ -61,6 +61,17 @@ void CipherData::Transpose(const std::vector &in, } } +void CipherData::ValidateKeyword(const std::string &message) +{ + if (keyWord_.size() < message.size()) return; + // Pop letters from keyWord until it is < message.size() + while (keyWord_.size() >= message.size()) keyWord_.pop_back(); + // Do not append order to a previous orderVect; Erase it first + orderVect_.erase(orderVect_.begin(), orderVect_.end()); + // Reinitialize orderVect with a valid order for the new keyWord + InitOrder(keyWord_); +} + std::string CipherData::Encrypt(std::string message) { // If no message was provided, ask for one @@ -69,6 +80,7 @@ std::string CipherData::Encrypt(std::string message) std::getline(std::cin, message); std::cout << "Encrypting message \"" << message << "\"\n"; } + ValidateKeyword(message); std::string encryptedMessage; std::vector rows; @@ -95,7 +107,8 @@ std::string CipherData::Decrypt(std::string message) std::getline(std::cin, message); std::cout << "Decrypting message \"" << message << "\"\n"; } - std::string result; + ValidateKeyword(message); + std::string decryptedMessage; std::vector rows; // Split the message into rows equal to the length of our keyWord @@ -122,6 +135,6 @@ std::string CipherData::Decrypt(std::string message) std::vector transposed; Transpose(rows, transposed); - for (const auto &row : transposed) result += row; - return result; + for (const auto &row : transposed) decryptedMessage += row; + return decryptedMessage; } diff --git a/cpp/cryptography/columnar-transposition/lib-cipher.hpp b/cpp/cryptography/columnar-transposition/lib-cipher.hpp index 24aeed2..22d3ebd 100644 --- a/cpp/cryptography/columnar-transposition/lib-cipher.hpp +++ b/cpp/cryptography/columnar-transposition/lib-cipher.hpp @@ -41,6 +41,11 @@ struct CipherData { std::string Decrypt(std::string message=""); private: + // Used to make a valid keyword from an invalid keyWord + // + Still encrypts / decrypts with whatever the user inputs as keyWord + // + But technically the keyword is modified without the user knowing :) + void ValidateKeyword(const std::string &message); + std::string keyWord_; std::vector orderVect_; };