Advertisement
saleks28

kmzi2_main

Jan 29th, 2020
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 KB | None | 0 0
  1. #include "feistel.h"
  2. #include "main.h"
  3. #include <time.h>
  4. #include <bitset>
  5.  
  6. size_t errorTimeCounter = -1;
  7.  
  8. void PrintMenu(size_t* userChoice)
  9. {
  10.      std::cout << "//-------------------------//" << std::endl;
  11.      std::cout << "1 - Encrypt" << std::endl;
  12.      std::cout << "2 - Decrypt" << std::endl;
  13.      std::cout << "3 - Calculate error propogation time" << std::endl;
  14.      std::cout << "//-------------------------//" << std::endl << ">";
  15.      std::cin >> *userChoice;
  16.      std::cin.ignore(32767, '\n');
  17. }
  18.  
  19. std::string GetFileName()
  20. {
  21.      std::string bufferString;
  22.      std::cout << "Enter name of file: ";
  23.      std::cin >> bufferString;
  24.      return bufferString;
  25. }
  26.  
  27. std::vector<uint8_t> ReadFile()
  28. {
  29.      std::vector<uint8_t> plainText;
  30.      std::ifstream fileStream(GetFileName(), std::ios::binary);
  31.  
  32.      while (!fileStream)
  33.      {
  34.           std::cout << "Wrong file" << std::endl;
  35.           fileStream.open(GetFileName());
  36.      }
  37.  
  38.      fileStream.seekg(0, std::ios::end);
  39.      plainText.reserve(static_cast<size_t>(fileStream.tellg()));
  40.      fileStream.seekg(0, std::ios::beg);
  41.  
  42.      fileStream >> std::noskipws;
  43.  
  44.      for (size_t i = 0; i < plainText.capacity(); ++i)
  45.      {
  46.           unsigned char sym;
  47.           fileStream >> sym;
  48.           plainText.push_back(sym);
  49.      }
  50.      
  51.      fileStream.close();
  52.  
  53.      return plainText;
  54. }
  55.  
  56. void WriteResult(std::vector<uint8_t> result)
  57. {
  58.      std::vector<uint8_t> data;
  59.      std::ofstream fileStream(GetFileName(), std::ios::binary);
  60.  
  61.      if (fileStream.is_open())
  62.      {
  63.           for (auto& sym : result)
  64.           {
  65.                fileStream << sym;
  66.           }
  67.      }
  68.      fileStream.close();
  69. }
  70.  
  71. void CalcPropogationTime(time_t& timeStart, time_t& timeEnd)
  72. {
  73.      std::vector< std::vector<uint8_t> > roundKeys = feistel::CreateRoundKeys(feistel::GetKey());
  74.  
  75.      time(&timeStart);
  76.  
  77.      // 4 такта криптосистемы
  78.      for (size_t r = 0; r < 4; ++r)
  79.      {    
  80.           // Цикл от 0 до 31 бита, фиксируем i-й бит
  81.           for (size_t i = 0; i < 32; ++i)
  82.           {
  83.                uint32_t tempBlock = 0;
  84.                // Цикл по остальным значениям
  85.                for (uint32_t x = 0; x < UINT32_MAX; ++x)
  86.                {
  87.                     uint32_t x1 = x & (~(1 << i));
  88.                     uint32_t x2 = x | (1 << i);
  89.                     // std::cout << "x1 || x2: " << std::bitset<32>(x1) << " || " << std::bitset<32>(x2) << std::endl;
  90.  
  91.                     std::vector<uint8_t> y1{ static_cast<uint8_t>(x1 >> 24), static_cast<uint8_t>((x1 >> 16) & 0xFF),
  92.                          static_cast<uint8_t>((x1 >> 8) & 0XFF), static_cast<uint8_t>(x1 & 0xFF) };
  93.                     std::vector<uint8_t> y2{ static_cast<uint8_t>(x2 >> 24), static_cast<uint8_t>((x2 >> 16) & 0xFF),
  94.                          static_cast<uint8_t>((x2 >> 8) & 0XFF), static_cast<uint8_t>(x2 & 0xFF) };
  95.                    
  96.                     errorTimeCounter = r;
  97.                     y1 = feistel::FeistelNetwork(y1, roundKeys);
  98.                     y2 = feistel::FeistelNetwork(y2, roundKeys);
  99.                     errorTimeCounter = -1;
  100.                          
  101.                     uint32_t ui_y1 = 0; uint32_t ui_y2 = 0;
  102.                     for (size_t j = 0; j < 4; ++j)
  103.                     {
  104.                          ui_y1 += y1[j];
  105.                          ui_y1 <<= 8;
  106.                          ui_y2 += y2[j];
  107.                          ui_y2 <<= 8;
  108.                     }
  109.                    
  110.                     tempBlock |= (ui_y1 ^ ui_y2);
  111.                     if (tempBlock == 0xffffffff)
  112.                     {
  113.                          std::cout << "An essential variable i = " << i << "on round  " << r + 1 << std::endl;
  114.                          x = UINT32_MAX;
  115.                          i = 33;
  116.                     }
  117.  
  118.                     if (x % 1000000 == 0)
  119.                     {
  120.                          std::cout << "Iteraton " << x << std::endl;
  121.                     }
  122.                }
  123.                std::cout << "Not an essential variable i = " << i << "on round  " << r + 1 << " with value "
  124.                     << tempBlock << std::endl;;
  125.                break;
  126.           }
  127.      }
  128.  
  129.      time(&timeEnd);
  130. }
  131.  
  132. int main()
  133. {
  134.    
  135.      size_t userChoice = -1;
  136.      while (1)
  137.      {
  138.           PrintMenu(&userChoice);
  139.           switch (userChoice)
  140.           {
  141.           case 1:
  142.           {
  143.                std::vector<uint8_t> plainText;
  144.                std::vector<uint8_t> cipherText;
  145.  
  146.                plainText = ReadFile();
  147.  
  148.                if (plainText.size() == 0)
  149.                {
  150.                     std::cout << "Empty input file" << std::endl;
  151.                     break;
  152.                }
  153.  
  154.                feistel::EncryptFunction(plainText, cipherText);
  155.  
  156.                WriteResult(cipherText);
  157.  
  158.                break;
  159.           }
  160.           case 2:
  161.           {
  162.                std::vector<uint8_t> plainText;
  163.                std::vector<uint8_t> cipherText;
  164.  
  165.                cipherText = ReadFile();
  166.  
  167.                if (cipherText.size() == 0)
  168.                {
  169.                     std::cout << "Empty input file" << std::endl;
  170.                     break;
  171.                }
  172.  
  173.                feistel::DecryptFunction(plainText, cipherText);
  174.  
  175.                WriteResult(plainText);
  176.  
  177.                break;
  178.           }
  179.           case 3:
  180.           {
  181.                std::cout << "Started counting error propogation time" << std::endl;
  182.                time_t timeStart, timeEnd;
  183.                CalcPropogationTime(timeStart, timeEnd);
  184.                std::cout << "Error propogation calculation time: " << difftime(timeStart, timeEnd);
  185.                break;
  186.           }
  187.          
  188.           default:
  189.           {
  190.                std::cout << "Wrong command, try again." << std::endl;
  191.                break;
  192.           }
  193.           } ///< switch
  194.      }
  195.      return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement