Advertisement
A-G-D

helloworld.cpp

Oct 14th, 2021
1,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. /*
  2. *   helloworld.cpp
  3. */
  4. #include <iostream>
  5. #include <string>
  6. #include <functional>
  7. #include <algorithm>
  8. #include <unordered_map>
  9.  
  10.  
  11. class SecretMessage : private std::string
  12. {
  13.     private: typedef std::unordered_map<std::string, char> hashtable;
  14.     private: typedef std::function<void(char)> callback;
  15.  
  16.  
  17.     public: class Cypher : hashtable
  18.     {
  19.         friend class SecretMessage;
  20.  
  21.         private: uint8_t __chunk_size;
  22.  
  23.         public: Cypher(std::string const &key)
  24.             : hashtable()
  25.         {
  26.             parse(key);
  27.         }
  28.  
  29.         public: Cypher &parse(std::string const &key)
  30.         {
  31.             size_t key_offset = key.find(' ');
  32.             uint8_t size = (uint8_t) std::stoi(key.substr(0, key_offset));
  33.             uint8_t ichar = 0;
  34.             __chunk_size = size;
  35.             for (size_t i = key_offset + 1; i < key.size(); i += size)
  36.             {
  37.                 std::string const &key_chunk = key.substr(i, size);
  38.                 insert({key_chunk, (char) ichar});
  39.                 ++ichar;
  40.             }
  41.             return *this;
  42.         }
  43.     };
  44.  
  45.  
  46.     public: class Printer : private std::string
  47.     {
  48.         public: Printer()
  49.             : std::string()
  50.         {
  51.         }
  52.  
  53.         public: Printer &print(char c)
  54.         {
  55.             if (size() == 0 || back() == ' ')
  56.                 c = toupper(c);
  57.             else
  58.                 c = tolower(c);
  59.             push_back(c);
  60.             std::cout << c;
  61.             return *this;
  62.         }
  63.     };
  64.  
  65.  
  66.     private: std::string __decrypted;
  67.  
  68.  
  69.     public: SecretMessage(std::string const &msg)
  70.         : std::string()
  71.     {
  72.         for (auto const &c : msg)
  73.             if (c != ' ')
  74.                 push_back(c);
  75.     }
  76.  
  77.  
  78.     public: SecretMessage &unlock(Cypher const &cypher)
  79.     {
  80.         __decrypted.clear();
  81.         for (size_t i = 0; i < size(); i += cypher.__chunk_size)
  82.         {
  83.             std::string const &chunk = substr(i, cypher.__chunk_size);
  84.             char c = cypher.at(chunk);
  85.             __decrypted.push_back(c);
  86.         }
  87.         return *this;
  88.     }
  89.  
  90.     public: SecretMessage &traverse_characters(callback on_traverse)
  91.     {
  92.         for (auto const &c : __decrypted)
  93.             on_traverse(c);
  94.         return *this;
  95.     }
  96. };
  97.  
  98.  
  99. int main()
  100. {
  101.     SecretMessage::Printer printer;
  102.  
  103.     SecretMessage(
  104.             "74OR!xE-tl6BF@~B"
  105.             "vD%dH!+vvD%dH!+v"
  106.             "3/c\\;c2t^-;n\\,"
  107.             "Z,jP4km65Qae+WF!"
  108.             "J/3/c\\;c2twbMAy"
  109.             "(3.vD%dH!+vTLH&V"
  110.             "Bf7XzvMJWC!     "
  111.         )
  112.         .unlock(
  113.             SecretMessage::Cypher(
  114.                 "8 DI[KtkwZmUf2E'Kc!*g)jpjO_Ti!@H"
  115.                 "P;=vpYo_X~`;`ew1]g9$I`psz#zG9(uQ"
  116.                 "Yy4jw'=oh\\q[sVgJmA6Rzze-ad~1N41"
  117.                 "#xeYEBm8;qXWZScPEgL/q,pfy^/Qtr~M"
  118.                 "t$F'MnoG$)P-SDuOq(D$JlDsfUTci+vi"
  119.                 "eV=]ko,+&p&NF\\a50%((`$HNC,3srm]"
  120.                 "rRcCd8t5AglNPaZ6xrum@B_Ol2LEHdyX"
  121.                 "SwS5,.=^dT4HF].Fv.OjC0XiR4s+&hxh"
  122.                 "bmDKjP4km65QXzvMJWC!I(kNclilR\\W"
  123.                 "=)GtYK#aUQjBz5c*[kSIa2u~/`KRxG3u"
  124.                 "waYbGl~Q9Y/NwLy0@(+#qe'J1_A79kV'"
  125.                 "J^aQ^^-;n\\,Z,#o]WBzo1;!h83]J$19"
  126.                 "bT6I6`),E_,ddrxWPPqF&)JX1#h3-so@"
  127.                 "^rW*=J[C50Z^y0AA!y4uWS%K#$7[AIh^"
  128.                 "UZ&)9M+2AIKUk_aHdG~Onb0N8b#L*@8g"
  129.                 "K+'D/oVpCfTi8un7VS/XF]im)C.7G6OY"
  130.                 "j.L9;iMn&%X`rnf5Gx0M14Zx-3[b_pg="
  131.                 "@*]nMfTLH&VBf7tl6BF@~BSeF-CVH'U_"
  132.                 "RjD~TR74OR!xE-uQq;2PvV.OTg*8.kb6"
  133.                 "3lz5'2vD%dH!+v\\%LQIh\\W*+(snN@*"
  134.                 "3/c\\;c2tBweE9%0hp&7LU=e%wbMAy(3"
  135.                 ".r)/t-v`8EsY7%\\[Uyf2q$Z![w1JsSX"
  136.                 "fKae+WF!J/3KzqE=M+[nO.QZm2zOPQU+"
  137.                 "=al6B7xW/9)t=+d[lHY9th(JgScSq6[t"
  138.                 "~@2L4,)biI4Wg&gjWWW.TZOhRtQ'[f]U"
  139.                 "Dn9DEOR#vm'=90fH]&5/H~~$8qr5$E@I"
  140.                 "*QN!`-^)L56$\\2i.TCyoNTmT@fD,8ao"
  141.                 "\\;PV0cKXs\\~BQlv'c1(epRz3V+'Zjp"
  142.                 ")`irp-u!Uh,ac*T1eK(dV_Aep%S^[+*2"
  143.                 "bn*7iSij/V7t$fZK@2eHTgBdCq~b*MI%"
  144.                 "'AL3LH_wFkn21idL901Ox.Vimr8'wRYX"
  145.                 "#zvNkUgWX-NnM\\[5$_mGgEw_=sKX3JH"
  146.                 "A_*G[Y&E7V4lu_"
  147.             )
  148.         )
  149.         .traverse_characters(
  150.             [&](char c){printer.print(c);}
  151.         );
  152.  
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement