Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstddef>
- #include <string>
- #include <string_view>
- #include <iostream>
- #include <vector>
- #include <assert.h>
- using namespace std;
- enum operation_type{
- XOR,
- ADD
- };
- struct operation{
- operation(const operation_type &type, const int value): type(type),value(value){;}
- operation_type type;
- int value;
- void operator=(const operation &other){
- this->type = other.type;
- this->value = other.value;
- }
- };
- typedef vector<operation> operations;
- std::ostream& operator<<(std::ostream &os, const operations ops){
- for(operation op: ops){
- os << (op.type ? "ADD":"XOR")<<" " << op.value << " ,";
- }
- os <<"\n";
- return os;
- }
- /*
- output vectorba rakja a kimenetet, brute force
- */
- void getAllPossibleOperations(int from, int to, int step, operations current,vector<operations> &output){
- if(step == 0){
- if(from==to)output.push_back(current);
- return;
- }
- //try XOR and ADD
- for(int i =0;i<=256;i++){
- operations currentcopy = current;
- currentcopy.push_back({operation_type::ADD,i});
- getAllPossibleOperations( (from+i)%256, to, step-1, currentcopy,output);
- }
- for(int i =0;i<=256;i++){
- operations currentcopy = current;
- currentcopy.push_back({operation_type::XOR,i});
- getAllPossibleOperations( (from^i)%256, to, step-1, currentcopy,output);
- }
- }
- string encrypt(const std::string &input,const operations &ops){
- string result = input;
- for(operation op : ops){
- for(int i =0;i<input.size();i++){
- if(result[i] == ' ' || result[i] == '\n')continue;
- if(op.type == operation_type::XOR){
- result[i] = result[i] ^ op.value;
- }else{
- result[i] = result[i] + op.value;
- }
- result[i] = result[i] % 256;
- }
- }
- return result;
- }
- operations findCipher(const std::string &input, const std::string &expectedOutput,const int step){
- vector<operations> possible;
- getAllPossibleOperations(input[0],expectedOutput[0],step,{},possible);
- for(operations ops : possible){
- try{
- string ret = encrypt(input,ops);
- if(ret == expectedOutput){
- return ops;
- }
- }catch(exception e){
- }
- }
- assert(false && "NOT VALID INPUT");
- }
- std::string messageTranslatorToDevelopers(std::string_view input, std::size_t steps) {
- operations ops;
- ops.push_back({operation_type::ADD,4});
- ops.push_back({operation_type::XOR,31});
- std::string inp(input.data());
- return encrypt(inp,ops);
- }
- int main() {
- constexpr std::size_t operationCount = 2;
- constexpr std::string_view exampleIn = "SZIASZTOK\n"
- "BOLDOG KARACSONYT\n"
- "MARKETING CSAPAT\n";
- constexpr std::string_view exampleOut = "HARZHAGLP\n"
- "YLOWLT PZIZXHLMBG\n"
- "NZIPVGRMT XHZKZG\n";
- constexpr std::string_view in = "SZIASZTOK\n"
- "KELLEMES HUSVETI UNNEPEKET\n"
- "MARKETING SUCCESS\n";
- if (messageTranslatorToDevelopers(exampleIn, operationCount) == exampleOut) {
- std::cout << messageTranslatorToDevelopers(in, operationCount) << std::flush;
- } else {
- std::cout << "example output not match!" << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement