Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "feistel.h"
- #include "main.h"
- #include <time.h>
- #include <bitset>
- size_t errorTimeCounter = -1;
- void PrintMenu(size_t* userChoice)
- {
- std::cout << "//-------------------------//" << std::endl;
- std::cout << "1 - Encrypt" << std::endl;
- std::cout << "2 - Decrypt" << std::endl;
- std::cout << "3 - Calculate error propogation time" << std::endl;
- std::cout << "//-------------------------//" << std::endl << ">";
- std::cin >> *userChoice;
- std::cin.ignore(32767, '\n');
- }
- 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;
- }
- void WriteResult(std::vector<uint8_t> result)
- {
- std::vector<uint8_t> data;
- std::ofstream fileStream(GetFileName(), std::ios::binary);
- if (fileStream.is_open())
- {
- for (auto& sym : result)
- {
- fileStream << sym;
- }
- }
- fileStream.close();
- }
- void CalcPropogationTime(time_t& timeStart, time_t& timeEnd)
- {
- std::vector< std::vector<uint8_t> > roundKeys = feistel::CreateRoundKeys(feistel::GetKey());
- time(&timeStart);
- // 4 такта криптосистемы
- for (size_t r = 0; r < 4; ++r)
- {
- // Цикл от 0 до 31 бита, фиксируем i-й бит
- for (size_t i = 0; i < 32; ++i)
- {
- uint32_t tempBlock = 0;
- // Цикл по остальным значениям
- for (uint32_t x = 0; x < UINT32_MAX; ++x)
- {
- uint32_t x1 = x & (~(1 << i));
- uint32_t x2 = x | (1 << i);
- // std::cout << "x1 || x2: " << std::bitset<32>(x1) << " || " << std::bitset<32>(x2) << std::endl;
- std::vector<uint8_t> y1{ static_cast<uint8_t>(x1 >> 24), static_cast<uint8_t>((x1 >> 16) & 0xFF),
- static_cast<uint8_t>((x1 >> 8) & 0XFF), static_cast<uint8_t>(x1 & 0xFF) };
- std::vector<uint8_t> y2{ static_cast<uint8_t>(x2 >> 24), static_cast<uint8_t>((x2 >> 16) & 0xFF),
- static_cast<uint8_t>((x2 >> 8) & 0XFF), static_cast<uint8_t>(x2 & 0xFF) };
- errorTimeCounter = r;
- y1 = feistel::FeistelNetwork(y1, roundKeys);
- y2 = feistel::FeistelNetwork(y2, roundKeys);
- errorTimeCounter = -1;
- uint32_t ui_y1 = 0; uint32_t ui_y2 = 0;
- for (size_t j = 0; j < 4; ++j)
- {
- ui_y1 += y1[j];
- ui_y1 <<= 8;
- ui_y2 += y2[j];
- ui_y2 <<= 8;
- }
- tempBlock |= (ui_y1 ^ ui_y2);
- if (tempBlock == 0xffffffff)
- {
- std::cout << "An essential variable i = " << i << "on round " << r + 1 << std::endl;
- x = UINT32_MAX;
- i = 33;
- }
- if (x % 1000000 == 0)
- {
- std::cout << "Iteraton " << x << std::endl;
- }
- }
- std::cout << "Not an essential variable i = " << i << "on round " << r + 1 << " with value "
- << tempBlock << std::endl;;
- break;
- }
- }
- time(&timeEnd);
- }
- int main()
- {
- size_t userChoice = -1;
- while (1)
- {
- PrintMenu(&userChoice);
- switch (userChoice)
- {
- case 1:
- {
- std::vector<uint8_t> plainText;
- std::vector<uint8_t> cipherText;
- plainText = ReadFile();
- if (plainText.size() == 0)
- {
- std::cout << "Empty input file" << std::endl;
- break;
- }
- feistel::EncryptFunction(plainText, cipherText);
- WriteResult(cipherText);
- break;
- }
- case 2:
- {
- std::vector<uint8_t> plainText;
- std::vector<uint8_t> cipherText;
- cipherText = ReadFile();
- if (cipherText.size() == 0)
- {
- std::cout << "Empty input file" << std::endl;
- break;
- }
- feistel::DecryptFunction(plainText, cipherText);
- WriteResult(plainText);
- break;
- }
- case 3:
- {
- std::cout << "Started counting error propogation time" << std::endl;
- time_t timeStart, timeEnd;
- CalcPropogationTime(timeStart, timeEnd);
- std::cout << "Error propogation calculation time: " << difftime(timeStart, timeEnd);
- break;
- }
- default:
- {
- std::cout << "Wrong command, try again." << std::endl;
- break;
- }
- } ///< switch
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement