Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "hash_function.h"
- uint32_t sourceSize = 0;
- ///@brief Функция для считывания имени файла
- ///
- std::string GetFileName( std::string purposeName )
- {
- std::string fileName;
- std::cout << "Enter filename to " << purposeName << ": ";
- std::cin >> fileName;
- std::cin.ignore(32767, '\n');
- return fileName;
- }
- ///@brief Функция для считывания ключа КС
- ///
- uint32_t GetFeistelKey()
- {
- std::ifstream fileStream( GetFileName("read key"), std::ios::binary | std::ios::in );
- uint32_t tempKey = 0;
- if ( fileStream.is_open() )
- {
- // Ключ 4 байта - проверим размер файла
- std::streampos fileSize;
- fileSize = fileStream.tellg();
- fileStream.seekg( 0, std::ios::end );
- fileSize = fileStream.tellg() - fileSize;
- fileStream.seekg( 0, std::ios::beg );
- fileStream >> std::noskipws;
- if ( static_cast<uint32_t>(fileSize) != 4 )
- {
- std::cout << "File length cannot be less than 4 bytes" << std::endl;
- return 0;
- }
- else
- {
- for ( size_t i = 0; i < 4; ++i )
- {
- tempKey += static_cast<uint8_t>( fileStream.get() );
- if (i != 3)
- tempKey <<= 8;
- }
- }
- }
- std::cout << "Key: " <<
- ((tempKey >> 24) & 0xFF) << " " << ((tempKey >> 16) & 0xFF) << " " <<
- ((tempKey >> 8) & 0xFF) << " " << (tempKey & 0xFF) << std::endl;
- fileStream.close();
- return tempKey;
- }
- ///@brief Функция для считывания открытого текста
- ///
- std::vector<uint8_t> ReadData( std::string fileName )
- {
- std::ifstream fileStream( fileName, std::ios::binary | std::ios::in);
- std::vector<uint8_t> data;
- if ( fileStream )
- {
- fileStream.seekg(0, std::ios::end);
- data.reserve(static_cast<size_t>(fileStream.tellg()));
- sourceSize = fileStream.tellg();
- fileStream.seekg(0, std::ios::beg);
- }
- fileStream >> std::noskipws;
- if ( fileStream.is_open() )
- {
- for ( size_t i = 0; i < data.capacity(); ++i )
- {
- uint8_t tempSym;
- fileStream >> tempSym;
- data.push_back( tempSym );
- }
- }
- fileStream.close();
- return data;
- }
- ///@brief Функция для записи шифртекста
- ///
- void WriteData( std::string fileName, std::vector<uint8_t> result )
- {
- std::ofstream fileStream(fileName, std::ios::binary | std::ios::out);
- if (fileStream.is_open())
- {
- for ( const uint8_t& sym : result )
- {
- fileStream << sym;
- }
- }
- fileStream.close();
- }
- int main()
- {
- // Данные КС
- const std::vector<uint8_t> S{ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 };
- uint32_t feistelKey = GetFeistelKey();
- // Объект КС
- CryptoSystem cs( S, feistelKey );
- // Считываем открытый текст
- std::vector<uint8_t> data = ReadData( GetFileName("for source text") );
- uint32_t hash;
- hash = cs.EncryptText( data );
- printf("Size file (L): %d ", sourceSize);
- printf("( %.2X %.2X %.2X %.2X )\n", (uint8_t)(sourceSize >> 24), (uint8_t)(sourceSize >> 16), (uint8_t)(sourceSize >> 8), (uint8_t)(sourceSize));
- printf("Hash: %u ", hash);
- printf("( %.2X %.2X %.2X %.2X )\n", (uint8_t)(hash >> 24), (uint8_t)(hash >> 16), (uint8_t)(hash >> 8), (uint8_t)(hash));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement