2021-07-20 15:21:59 +00:00
|
|
|
/*##############################################################################
|
|
|
|
## Author: Shaun Reed ##
|
|
|
|
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
|
|
|
## About: An example of using columnar transposition cipher ##
|
|
|
|
## + Using concept at www.braingle.com/brainteasers/codes/columnartrans.php ##
|
|
|
|
## ##
|
|
|
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
|
|
|
################################################################################
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COLUMNARTRANSPOSITION_LIB_CIPHER_HPP
|
|
|
|
#define COLUMNARTRANSPOSITION_LIB_CIPHER_HPP
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include<algorithm>
|
|
|
|
#include<vector>
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2021-07-22 14:50:29 +00:00
|
|
|
// KeyData struct to hold data related to key ciphers
|
|
|
|
struct KeyData {
|
|
|
|
explicit KeyData(std::string key) : keyWord_(std::move(key)) {};
|
|
|
|
explicit KeyData() : keyWord_(GetKey()) {};
|
|
|
|
// Pure virtual dtor to make abstract class despite no pure virtual members
|
|
|
|
virtual ~KeyData() = 0;
|
|
|
|
|
|
|
|
// Allows getting keyWord when using KeyData default ctor
|
|
|
|
static std::string GetKey()
|
|
|
|
{
|
|
|
|
std::string key;
|
|
|
|
std::cout << "Enter the keyword: ";
|
|
|
|
std::getline(std::cin, key);
|
|
|
|
std::cout << "Keyword entered: \"" << key << "\"\n";
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::string keyWord_;
|
|
|
|
};
|
|
|
|
// Definition of pure virtual dtor
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// Columnar struct to define methods related to columnar transposition
|
|
|
|
struct Columnar : KeyData {
|
2021-07-20 15:21:59 +00:00
|
|
|
// Can't create this object without providing a keyWord to initialize order
|
2021-07-22 14:50:29 +00:00
|
|
|
explicit Columnar(std::string keyWord) : KeyData(std::move(keyWord))
|
2021-07-20 15:21:59 +00:00
|
|
|
{
|
|
|
|
InitOrder(keyWord_);
|
|
|
|
}
|
2021-07-22 14:50:29 +00:00
|
|
|
// Inherit call to GetKey() from KeyData default ctor
|
|
|
|
Columnar() { InitOrder(keyWord_);}
|
2021-07-20 15:21:59 +00:00
|
|
|
|
2021-07-22 14:50:29 +00:00
|
|
|
// Initializes orderVect to determine order columns should be used
|
2021-07-20 15:21:59 +00:00
|
|
|
void InitOrder(std::string temp);
|
|
|
|
|
|
|
|
|
2021-07-22 14:50:29 +00:00
|
|
|
/****************************************************************************/
|
|
|
|
// Member function definitions
|
|
|
|
|
|
|
|
void ValidateKeyword(const std::string &message);
|
|
|
|
|
|
|
|
// Transpose a character matrix, using a vector of strings
|
|
|
|
static void Transpose(const std::vector<std::string> &in,
|
|
|
|
std::vector<std::string> &out);
|
2021-07-20 15:21:59 +00:00
|
|
|
|
2021-07-22 14:50:29 +00:00
|
|
|
// Default parameter allows calling Encrypt() to take message input
|
2021-07-20 15:21:59 +00:00
|
|
|
std::string Encrypt(std::string message="");
|
2021-07-22 14:50:29 +00:00
|
|
|
// Default parameter allows calling Decrypt() to take message input
|
2021-07-20 15:21:59 +00:00
|
|
|
std::string Decrypt(std::string message="");
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<int> orderVect_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // COLUMNARTRANSPOSITION_LIB_CIPHER_HPP
|