2021-04-23 21:31:42 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2021-04-26 10:11:08 +02:00
|
|
|
#include <boost/program_options.hpp>
|
2021-04-26 21:29:58 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-04-26 23:04:21 +02:00
|
|
|
#include <time.h>
|
2021-04-26 10:11:08 +02:00
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2021-04-26 23:04:21 +02:00
|
|
|
std::string randomkey()
|
|
|
|
{
|
|
|
|
std::string sAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
|
srand(time(NULL)); // init srand with seed as current system clock time
|
|
|
|
std::string sKey;
|
|
|
|
for (int i = 0; i != 10; i++)
|
|
|
|
{
|
|
|
|
int iRandomIndex = rand() % 51 +1; // generate a number between 1 and 26
|
|
|
|
sKey.insert(sKey.length(), 1, sAlphabet[iRandomIndex]);
|
|
|
|
}
|
|
|
|
return sKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> parseargs(int argc, char* argv[])
|
2021-04-26 10:11:08 +02:00
|
|
|
{
|
2021-04-26 21:51:25 +02:00
|
|
|
std::string sIFile;
|
2021-04-26 23:04:21 +02:00
|
|
|
std::string bInputHex = "false";
|
2021-04-26 21:51:25 +02:00
|
|
|
std::string sOFile;
|
2021-04-26 23:04:21 +02:00
|
|
|
std::string bOutputHex = "false";
|
2021-04-26 21:51:25 +02:00
|
|
|
std::string sKey;
|
2021-04-26 21:29:58 +02:00
|
|
|
|
2021-04-26 10:11:08 +02:00
|
|
|
po::options_description desc("Allowed options");
|
|
|
|
desc.add_options()
|
|
|
|
("help", "Print this message")
|
2021-04-26 21:33:45 +02:00
|
|
|
("if", po::value<std::string>(), "Specify an input file")
|
2021-04-26 10:11:08 +02:00
|
|
|
("ih", "Input data is in hex")
|
2021-04-26 21:33:45 +02:00
|
|
|
("of", po::value<std::string>(), "Specify an output file")
|
2021-04-26 10:11:08 +02:00
|
|
|
("oh", "Output data in hex")
|
2021-04-26 21:51:25 +02:00
|
|
|
("k", po::value<std::string>(), "Specify a key")
|
2021-04-26 10:11:08 +02:00
|
|
|
;
|
|
|
|
|
2021-04-26 21:33:45 +02:00
|
|
|
/*po::positional_options_description p;
|
|
|
|
p.add("input", -1);*/
|
2021-04-26 10:11:08 +02:00
|
|
|
|
|
|
|
po::variables_map vm;
|
|
|
|
po::store(po::parse_command_line(argc, argv, desc), vm);
|
|
|
|
po::notify(vm);
|
|
|
|
|
|
|
|
if (vm.count("help"))
|
|
|
|
{
|
|
|
|
std::cout << desc << std::endl;
|
|
|
|
}
|
|
|
|
|
2021-04-26 21:33:45 +02:00
|
|
|
if (vm.count("if"))
|
2021-04-26 10:11:08 +02:00
|
|
|
{
|
2021-04-26 21:51:25 +02:00
|
|
|
sIFile = vm["if"].as<std::string>();
|
|
|
|
sIFile = sIFile.substr(1, sIFile.length());
|
2021-04-26 10:11:08 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 21:29:58 +02:00
|
|
|
if (vm.count("ih"))
|
2021-04-26 10:11:08 +02:00
|
|
|
{
|
2021-04-26 23:04:21 +02:00
|
|
|
bool bInputHex = "true";
|
2021-04-26 10:11:08 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 21:33:45 +02:00
|
|
|
if (vm.count("of"))
|
2021-04-26 10:11:08 +02:00
|
|
|
{
|
2021-04-26 21:51:25 +02:00
|
|
|
sOFile = vm["of"].as<std::string>();
|
|
|
|
sOFile = sOFile.substr(1, sOFile.length());
|
2021-04-26 10:11:08 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 21:29:58 +02:00
|
|
|
if (vm.count("oh"))
|
2021-04-26 10:11:08 +02:00
|
|
|
{
|
2021-04-26 23:04:21 +02:00
|
|
|
bool bOutputHex = "true";
|
2021-04-26 21:29:58 +02:00
|
|
|
}
|
2021-04-26 21:51:25 +02:00
|
|
|
|
|
|
|
if (vm.count("k"))
|
|
|
|
{
|
|
|
|
sKey = vm["k"].as<std::string>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-26 23:04:21 +02:00
|
|
|
std::string sKey = randomkey();
|
|
|
|
std::cout << "No key provided, using generated key " << sKey << std::endl;
|
2021-04-26 21:51:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 23:04:21 +02:00
|
|
|
std::vector<std::string> sParsedArgs {sIFile, bInputHex, sOFile, bOutputHex, sKey}; // init vector with parsed args
|
|
|
|
return sParsedArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void encryptio(std::vector<std::string> sParsedArgs)
|
|
|
|
{
|
|
|
|
std::string sMessage;
|
|
|
|
|
|
|
|
if (sParsedArgs[0] == "") // if no file path was provided
|
|
|
|
{
|
|
|
|
std::cout << "No file path provided, please input message:";
|
|
|
|
std::cin >> sMessage;
|
|
|
|
}
|
|
|
|
else // if a file path was provided
|
|
|
|
{
|
|
|
|
// TODO WRITE HEX CASE TO CONVERT HEX BACK TO ORIGINAL FORM!!!!!!
|
|
|
|
std::ifstream IFile;
|
|
|
|
IFile.open(sParsedArgs[0]); // open file
|
|
|
|
IFile >> sMessage;
|
|
|
|
}
|
2021-04-26 10:11:08 +02:00
|
|
|
}
|
2021-04-23 21:31:42 +02:00
|
|
|
|
2021-04-26 21:29:58 +02:00
|
|
|
std::vector<char> xormessage(std::string sMessage, std::string sKey)
|
2021-04-23 21:31:42 +02:00
|
|
|
{
|
2021-04-26 21:29:58 +02:00
|
|
|
std::vector<char> cXORMessage;
|
|
|
|
int iKeyIndex = 0;
|
2021-04-26 10:11:08 +02:00
|
|
|
|
2021-04-26 21:29:58 +02:00
|
|
|
for (int iMessageIndex = 0; iMessageIndex < sMessage.length(); iMessageIndex++)
|
2021-04-23 21:31:42 +02:00
|
|
|
{
|
2021-04-26 21:29:58 +02:00
|
|
|
cXORMessage.push_back(sMessage[iMessageIndex] ^ sKey[iKeyIndex]);
|
|
|
|
iKeyIndex++;
|
|
|
|
if (iKeyIndex == sKey.length())
|
2021-04-23 21:31:42 +02:00
|
|
|
{
|
2021-04-26 21:29:58 +02:00
|
|
|
iKeyIndex = 0;
|
2021-04-23 21:31:42 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-26 21:29:58 +02:00
|
|
|
return cXORMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2021-04-26 23:04:21 +02:00
|
|
|
std::vector<std::string> sParsedArgs = parseargs(argc, argv);
|
|
|
|
encryptio(sParsedArgs);
|
2021-04-26 21:29:58 +02:00
|
|
|
xormessage("hello ", "key");
|
|
|
|
|
|
|
|
|
2021-04-23 21:31:42 +02:00
|
|
|
}
|