Toliak

___2

Dec 19th, 2018
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. uint32_t cShiftR(uint32_t number)
  8. {
  9.     uint32_t mask = 0b111;
  10.     return (number >> 3) | ((number & mask) << 29);
  11. }
  12.  
  13. uint32_t cShiftL(uint32_t number)
  14. {
  15.     uint32_t mask = 0b111;
  16.     mask <<= 29;
  17.     return (number << 3) | ((number & mask) >> 29);
  18. }
  19.  
  20.  
  21. void decode(vector<uint32_t> data)
  22. {
  23.     int key;
  24.     cout << "Enter key: ";
  25.     cin >> key;
  26.  
  27.     if (data.size() == 0) {
  28.         string str;
  29.         cin.ignore();
  30.         getline(cin, str);
  31.  
  32.         if (str.size() % 4 != 0) {
  33.             cout << "Wrong string" << endl;
  34.             return;
  35.         }
  36.  
  37.         int length = str.size() / 4;
  38.         data = vector<uint32_t>(length, 0);
  39.         for (int i = 0; i < length * 4; i++) {
  40.             data[i / 4] |= str[i] << 8 * (i % 4);
  41.         }
  42.     }
  43.  
  44.     int length = data.size();
  45.     srand(key);
  46.     for (int i = 0; i < length; i++) {
  47.         uint32_t gamma = (rand() << 16) | rand();
  48.         data[i] = cShiftR(data[i]) ^ gamma;
  49.     }
  50.  
  51.     cout << "Decoded: ";
  52.     for (int i = 0; i < length; i++)
  53.         for (int j = 0; j < 4; j++) {
  54.             char c = (char)(data[i] >> 8 * j);
  55.             if (c != '\12')
  56.                 cout << c;
  57.         }
  58.     cout << endl;
  59. }
  60.  
  61. void encode()
  62. {
  63.     int key;
  64.     cout << "Enter key: ";
  65.     cin >> key;
  66.  
  67.     string str;
  68.     cout << "Enter string: ";
  69.     cin.ignore();
  70.     getline(cin, str);
  71.  
  72.     int length = str.size() / 4 + (str.size() % 4 != 0);
  73.     vector<uint32_t> encoded(length, 0);
  74.     for (int i = 0; i < length * 4; i++) {
  75.         char c = ((str.size() > i) ? str[i] : '\12');
  76.         encoded[i / 4] |= c << 8 * (i % 4);
  77.     }
  78.  
  79.     srand(key);
  80.     for (int i = 0; i < length; i++) {
  81.         uint32_t gamma = (rand() << 16) | rand();
  82.         encoded[i] = cShiftL(encoded[i] ^ gamma);
  83.     }
  84.  
  85.     cout << "Encoded: ";
  86.     for (int i = 0; i < length; i++)
  87.         for (int j = 0; j < 4; j++)
  88.             cout << (char)(encoded[i] >> 8 * j);
  89.     cout << endl;
  90.  
  91.     decode(encoded);
  92. }
  93.  
  94. int main()
  95. {
  96.     cout << "Encode (0) or decode (1): ";
  97.     int state;
  98.     cin >> state;
  99.     if (state == 0) {
  100.         encode();
  101.     } else {
  102.         decode({});
  103.     }
  104.  
  105.     return 0;
  106. }
Add Comment
Please, Sign In to add comment