Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "feistel.h"
- #include "main.h"
- #include <time.h>
- std::string GetFileName()
- {
- std::string bufferString;
- std::cout << "Enter name of file: ";
- std::cin >> bufferString;
- return bufferString;
- }
- std::vector<uint8_t> ReadFile()
- {
- std::vector<uint8_t> plainText;
- std::ifstream fileStream(GetFileName(), std::ios::binary);
- while (!fileStream)
- {
- std::cout << "Wrong file" << std::endl;
- fileStream.open(GetFileName());
- }
- fileStream.seekg(0, std::ios::end);
- plainText.reserve(static_cast<size_t>(fileStream.tellg()));
- fileStream.seekg(0, std::ios::beg);
- fileStream >> std::noskipws;
- for (size_t i = 0; i < plainText.capacity(); ++i)
- {
- unsigned char sym;
- fileStream >> sym;
- plainText.push_back(sym);
- }
- fileStream.close();
- return plainText;
- }
- int main()
- {
- time_t startTime, endTime;
- std::cout << "Plain text 1" << std::endl;
- const std::vector<uint8_t> plainText1 = ReadFile();
- std::cout << "Plain text 2" << std::endl;
- const std::vector<uint8_t> plainText2 = ReadFile();
- std::cout << "Ecnrypted text 1" << std::endl;
- const std::vector<uint8_t> encryptedText1= ReadFile();
- std::cout << "Encrypted text 2" << std::endl;
- const std::vector<uint8_t> encryptedText2 = ReadFile();
- const uint32_t c = 40832;
- time(&startTime);
- // Простые соотношения
- // g1(X) = X ^ const
- // g2(X) = X ^ const
- // h(X) = X ^ const
- std::vector<uint8_t> X1{ plainText1[0], plainText1[1], plainText1[2], plainText1[3] };
- std::vector<uint8_t> g1_X1{ plainText2[0], plainText2[1], plainText2[2], plainText2[3] };
- std::vector<uint8_t> Y1{ encryptedText1[0], encryptedText1[1], encryptedText1[2], encryptedText1[3] };
- std::vector<uint8_t> Y2{ encryptedText2[0], encryptedText2[1], encryptedText2[2], encryptedText2[3] };
- // for
- for (uint32_t key = 0; key < UINT32_MAX; ++key)
- {
- if(key % 500000 == 0)
- std::cout << std::endl << key << std::endl;;
- // Если h(theta) < theta, значит этот ключ уже проверяли, идем дальше
- if ( (key ^ c) < key )
- {
- continue;
- }
- std::vector<uint8_t> vKey{ static_cast<uint8_t>(key >> 24), static_cast<uint8_t>((key >> 16) & 0xFF),
- static_cast<uint8_t>((key >> 8) & 0XFF), static_cast<uint8_t>(key & 0xFF) };
- std::vector<uint8_t> vhKey{ static_cast<uint8_t>((key ^ c) >> 24), static_cast<uint8_t>(((key ^ c) >> 16) & 0xFF),
- static_cast<uint8_t>(((key ^ c) >> 8) & 0XFF), static_cast<uint8_t>((key ^ c) & 0xFF) };
- std::vector<uint8_t> Y_;
- feistel::EncryptFunction(X1, Y_, vKey);
- for(size_t i = 0; i < 4; ++i)
- Y_.pop_back();
- // Первая проверка
- if (Y1 == Y_)
- {
- std:: cout << "Found key: " << vKey[0] << vKey[1] << vKey[2] << vKey[3] << std::endl;
- time(&endTime);
- std::cout <<"Time: " << difftime(endTime, startTime) << std::endl;
- return 0;
- }
- // Вторая проверка
- std::vector<uint8_t> Y2_buf(4);
- Y2_buf[0] = Y2[0] ^ static_cast<uint8_t> (c >> 24);
- Y2_buf[1] = Y2[1] ^ static_cast<uint8_t> ((c >> 16) & 0XFF );
- Y2_buf[2] = Y2[2] ^ static_cast<uint8_t> ((c >> 8) & 0XFF);
- Y2_buf[3] = Y2[3] ^ static_cast<uint8_t> (c & 0xFF);
- if (Y2_buf == Y_)
- {
- std:: cout << "Found key: " << vhKey[0] << vhKey[1] << vhKey[2] << vhKey[3] << std::endl;
- time(&endTime);
- std::cout <<"Time: " << difftime(endTime, startTime) << std::endl;
- return 0;
- }
- }
- time(&endTime);
- std::cout <<"Time: " << difftime(endTime, startTime) << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement