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 ##
|
|
|
|
################################################################################
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lib-cipher.hpp"
|
|
|
|
|
|
|
|
#include<iostream>
|
|
|
|
|
|
|
|
// Columnar transposition cipher
|
|
|
|
// https://www.braingle.com/brainteasers/codes/columnartrans.php#form
|
|
|
|
// NOTE: Must check the 'irregular case' box to produce same results
|
|
|
|
// + I only used the irregular case to encrypt and decrypt
|
|
|
|
// + I did not write any code for testing with the regular case
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// Main program loop
|
|
|
|
int main (const int argc, const char * argv[]) {
|
|
|
|
// Left some test cases I used commented out :)
|
2021-07-20 20:04:18 +00:00
|
|
|
// + Keywords and messages can be URLs, include spaces, symbols, numbers, etc
|
2021-07-20 15:21:59 +00:00
|
|
|
|
|
|
|
// Using example keyword from www.braingle.com
|
|
|
|
// + The embedded example there doesn't seem to support numbers in keywords :(
|
2021-07-22 14:50:29 +00:00
|
|
|
Columnar cData("ZEBRAS");
|
2021-07-20 15:21:59 +00:00
|
|
|
|
2021-07-22 14:50:29 +00:00
|
|
|
// Taking user input for keyword using Columnar ctor
|
|
|
|
// Columnar cData;
|
2021-07-20 15:21:59 +00:00
|
|
|
|
|
|
|
int choice = 0;
|
|
|
|
bool exit = false;
|
|
|
|
std::string message; // Message to encrypt or decrypt
|
|
|
|
while (!exit) {
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
switch (choice) {
|
|
|
|
case 1:
|
|
|
|
std::cout << "\nEncrypting...\n";
|
|
|
|
|
|
|
|
// Encrypt example from www.braingle.com
|
|
|
|
result = cData.Encrypt("WEAREDISCOVEREDFLEEATONCE");
|
|
|
|
|
|
|
|
// Take input for encrypting a message
|
|
|
|
// result = cData.Encrypt();
|
|
|
|
|
2021-07-20 20:04:18 +00:00
|
|
|
std::cout << "Encrypted message: \"" << result << "\"\n";
|
2021-07-20 15:21:59 +00:00
|
|
|
exit = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
std::cout << "\nDecrypting...\n";
|
|
|
|
|
|
|
|
// Encrypt, decrypt example from www.braingle.com
|
|
|
|
result = cData.Decrypt(cData.Encrypt("WEAREDISCOVEREDFLEEATONCE"));
|
|
|
|
|
|
|
|
// Take input for previously encrypted message to decrypt
|
|
|
|
// result = cData.Decrypt();
|
|
|
|
|
2021-07-20 20:04:18 +00:00
|
|
|
std::cout << "Decrypted message: \"" << result << "\"\n";
|
2021-07-20 15:21:59 +00:00
|
|
|
exit = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
std::cout << "What would you like to do?\n1. Encrypt\n2. Decrypt\n";
|
|
|
|
// Take input, handle it accordingly
|
|
|
|
// + https://www.cplusplus.com/forum/general/207458/
|
|
|
|
if (!(std::cin >> choice)) {
|
|
|
|
std::cout << "Invalid character input, choose an option below\n";
|
|
|
|
}
|
|
|
|
else if (choice != 1 && choice != 2) {
|
|
|
|
std::cout << "Incorrect option entered (" << choice << ")\n";
|
|
|
|
}
|
|
|
|
std::cin.clear();
|
|
|
|
std::cin.ignore();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|