Advertisement
saleks28

kmzi4

Jan 29th, 2020
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include "main.h"
  2. #include "feistel.h"
  3.  
  4. std::string GetFileName()
  5. {
  6.      std::string bufferString;
  7.      std::cout << "Enter name of file: ";
  8.      std::cin >> bufferString;
  9.  
  10.      return bufferString;
  11. }
  12.  
  13. std::vector<uint8_t> ReadFile()
  14. {
  15.      std::vector<uint8_t> plainText;
  16.      std::ifstream fileStream(GetFileName(), std::ios::binary);
  17.  
  18.      if (!fileStream)
  19.      {
  20.           std::cout << "Wrong file" << std::endl;
  21.           return plainText;
  22.      }
  23.  
  24.      fileStream.seekg(0, std::ios::end);
  25.      plainText.reserve(fileStream.tellg());
  26.      fileStream.seekg(0, std::ios::beg);
  27.  
  28.      for (size_t i = 0; i < plainText.capacity(); ++i)
  29.      {
  30.           unsigned char sym;
  31.           fileStream >> sym;
  32.           plainText.push_back(sym);
  33.      }
  34.  
  35.      fileStream.close();
  36.  
  37.      return plainText;
  38. }
  39.  
  40. void WriteResult(std::vector<uint8_t> result)
  41. {
  42.      std::cout << "Result file" << std::endl;
  43.      std::vector<uint8_t> data;
  44.      std::ofstream fileStream(GetFileName(), std::ios::binary);
  45.  
  46.      if (fileStream.is_open())
  47.      {
  48.           for (auto& sym : result)
  49.           {
  50.                fileStream << sym;
  51.           }
  52.      }
  53.      fileStream.close();
  54. }
  55.  
  56. int main()
  57. {
  58.      std::vector<uint8_t> synchroPackage;
  59.      std::vector<uint8_t> sourceText;
  60.  
  61.      std::cout << "SynchroPackage" << std::endl;
  62.      synchroPackage = ReadFile();
  63.      std::cout << "Source text" << std::endl;
  64.      sourceText = ReadFile();
  65.  
  66.      if (synchroPackage.empty() || sourceText.empty())
  67.      {
  68.           std::cout << "One of input files is empty" << std::endl;
  69.           return 1;
  70.      }
  71.      if (synchroPackage.size() != 4)
  72.      {
  73.           std::cout << "SynchroPackage must be 4 symbols precisely" << std::endl;
  74.           return 1;
  75.      }
  76.      
  77.      std::cout << "Choose operation: " << std::endl;
  78.      std::cout << "1 - Encryption" << std::endl;
  79.      std::cout << "2 - Decryption" << std::endl << std::endl;
  80.      std::cout << ">";
  81.      size_t userChoice;
  82.      std::cin >> userChoice;
  83.      std::cin.ignore(32767, '\n');
  84.  
  85.      if (userChoice == 1)
  86.      {
  87.           WriteResult(feistel::OFB(synchroPackage, sourceText, feistel::ENCRYPT_OPER));
  88.      }
  89.      else if (userChoice == 2)
  90.      {
  91.           WriteResult(feistel::OFB(synchroPackage, sourceText, feistel::DECRYPT_OPER));
  92.      }
  93.      return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement