Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include <cstddef>
  2. #include <string>
  3. #include <string_view>
  4. #include <iostream>
  5. #include <vector>
  6. #include <assert.h>
  7.  
  8. using namespace std;
  9. enum operation_type{
  10.     XOR,
  11.     ADD
  12. };
  13.  
  14. struct operation{
  15.     operation(const operation_type &type, const int value): type(type),value(value){;}
  16.     operation_type type;
  17.     int value;
  18.     void operator=(const operation &other){
  19.         this->type = other.type;
  20.         this->value = other.value;
  21.     }
  22. };
  23.  
  24.  
  25. typedef vector<operation> operations;
  26.  
  27. std::ostream& operator<<(std::ostream &os, const operations ops){
  28.     for(operation op: ops){
  29.         os << (op.type ? "ADD":"XOR")<<" " << op.value << " ,";
  30.     }
  31.     os <<"\n";
  32.     return os;
  33. }
  34.  
  35.  
  36. /*
  37. output vectorba rakja a kimenetet, brute force
  38. */
  39. void getAllPossibleOperations(int from, int to, int step, operations current,vector<operations> &output){
  40.     if(step == 0){
  41.         if(from==to)output.push_back(current);
  42.         return;
  43.     }
  44.  
  45.     //try XOR and ADD
  46.     for(int i =0;i<=256;i++){
  47.         operations currentcopy = current;
  48.         currentcopy.push_back({operation_type::ADD,i});
  49.         getAllPossibleOperations( (from+i)%256, to, step-1, currentcopy,output);
  50.     }
  51.  
  52.     for(int i =0;i<=256;i++){
  53.         operations currentcopy = current;
  54.         currentcopy.push_back({operation_type::XOR,i});
  55.         getAllPossibleOperations( (from^i)%256, to, step-1, currentcopy,output);
  56.     }
  57. }
  58.  
  59. string encrypt(const std::string &input,const operations &ops){
  60.     string result = input;
  61.  
  62.     for(operation op : ops){
  63.         for(int i =0;i<input.size();i++){
  64.             if(result[i] == ' ' || result[i] == '\n')continue;
  65.             if(op.type == operation_type::XOR){
  66.                 result[i] = result[i] ^ op.value;
  67.             }else{
  68.                 result[i] = result[i] + op.value;
  69.             }
  70.            
  71.             result[i] = result[i] % 256;
  72.         }
  73.     }
  74.  
  75.     return result;
  76. }
  77.  
  78.  
  79.  
  80. operations findCipher(const std::string &input, const std::string &expectedOutput,const int step){
  81.     vector<operations> possible;
  82.     getAllPossibleOperations(input[0],expectedOutput[0],step,{},possible);
  83.    
  84.  
  85.     for(operations ops : possible){
  86.         try{
  87.             string ret = encrypt(input,ops);
  88.  
  89.             if(ret == expectedOutput){
  90.                 return ops;
  91.             }
  92.         }catch(exception e){
  93.  
  94.         }  
  95.     }
  96.  
  97.     assert(false && "NOT VALID INPUT");
  98. }
  99.  
  100.  
  101. std::string messageTranslatorToDevelopers(std::string_view input, std::size_t steps) {
  102.     operations ops;
  103.     ops.push_back({operation_type::ADD,4});
  104.     ops.push_back({operation_type::XOR,31});
  105.     std::string inp(input.data());
  106.     return encrypt(inp,ops);
  107. }
  108.  
  109. int main() {
  110.     constexpr std::size_t operationCount = 2;
  111.     constexpr std::string_view exampleIn = "SZIASZTOK\n"
  112.         "BOLDOG KARACSONYT\n"
  113.         "MARKETING CSAPAT\n";
  114.  
  115.     constexpr std::string_view exampleOut = "HARZHAGLP\n"
  116.         "YLOWLT PZIZXHLMBG\n"
  117.         "NZIPVGRMT XHZKZG\n";
  118.  
  119.     constexpr std::string_view in = "SZIASZTOK\n"
  120.         "KELLEMES HUSVETI UNNEPEKET\n"
  121.         "MARKETING SUCCESS\n";
  122.  
  123.     if (messageTranslatorToDevelopers(exampleIn, operationCount) == exampleOut) {
  124.         std::cout << messageTranslatorToDevelopers(in, operationCount) << std::flush;
  125.     } else {
  126.         std::cout << "example output not match!" << std::endl;
  127.     }
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement