Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "hash_function.h"
- extern uint32_t sourceSize;
- CryptoSystem::CryptoSystem( const std::vector<uint8_t>& S, const uint32_t& key ) :
- S_(S), feistelKey_(key)
- {
- }
- uint32_t CryptoSystem::FormBlock( const std::vector<uint8_t>& bytes )
- {
- uint32_t tempBlock = 0;
- for ( size_t i = 0; i < 4; ++i )
- {
- tempBlock += bytes[i];
- if (i != 3)
- {
- tempBlock <<= 8;
- }
- }
- return tempBlock;
- }
- std::vector<uint8_t> CryptoSystem::DeformBlock(const uint32_t& block )
- {
- std::vector<uint8_t> deformedBlock =
- {
- static_cast<uint8_t>(( block >> 24) & 0xFF ),
- static_cast<uint8_t>((block >> 16) & 0xFF),
- static_cast<uint8_t>((block >> 8) & 0xFF),
- static_cast<uint8_t>(block & 0xFF)
- };
- return deformedBlock;
- }
- uint32_t CryptoSystem::EncryptBlock( const uint32_t& block, uint32_t key )
- {
- uint32_t result = 0;
- // Раундовые ключи
- const uint16_t theta1 = key >> 16;
- const uint16_t theta2 = ( key << 16 ) >> 16;
- // Ключ отбеливания
- const uint32_t thetaWhitening = ( ( theta1 ^ theta2 ) << 16 ) | ( theta1 ^ theta2 );
- // 4 Раунда
- result = FeistelRound( block, theta1 );
- result = FeistelRound( result, theta2 );
- result = FeistelRound( result, theta1 );
- result = FeistelRound( result, theta2 );
- // Тау
- result = ( result << 16 ) | ( result >> 16);
- // Отбеливание
- result ^= thetaWhitening;
- // Хэш Fx ^ X
- result ^= block;
- return result;
- }
- uint32_t CryptoSystem::SP(const uint16_t& x, const uint16_t& roundKey)
- {
- uint16_t res = x ^ roundKey;
- uint16_t sBlocks[4] = { 0, 0, 0, 0 };
- // Делим на блоки от 1-m, 2m-3m и т.д
- sBlocks[0] = res >> 12;
- sBlocks[1] = res << 4;
- sBlocks[1] >>= 12;
- sBlocks[2] = res << 8;
- sBlocks[2] >>= 12;
- sBlocks[3] = res << 12;
- sBlocks[3] >>= 12;
- // S
- for ( size_t i = 0; i < 4; ++i )
- {
- sBlocks[i] = S_[sBlocks[i]];
- }
- // P
- uint16_t P0 = sBlocks[3] | ( sBlocks[2] << 4 )
- | ( sBlocks[1] << 8 ) | ( sBlocks[0] << 12);
- uint16_t num1 = 0;
- res = 0;
- for ( int i = 0; i < 16; ++i )
- {
- num1 = P0 << i;
- num1 >>= 15;
- res += num1 * static_cast<uint16_t>(pow(2, 15 - ((13 * i + 1) % 16)));
- }
- return res;
- }
- uint32_t CryptoSystem::FeistelRound( const uint32_t& block, const uint16_t& roundKey )
- {
- uint32_t x1 = block >> 16;
- uint32_t x2 = block << 16;
- x2 >>= 16;
- uint16_t funcRes = SP( x2, roundKey );
- uint32_t result = ( x2 << 16 ) | ( x1 ^ funcRes );
- return result;
- }
- uint32_t CryptoSystem::EncryptText( std::vector<uint8_t> sourceText )
- {
- // Вектор, хранящий значение шифрования
- std::vector<uint8_t> encryptedText;
- while ( sourceText.size() % 4 != 0 )
- {
- sourceText.push_back(0);
- }
- // Считывание и шифрования 1 блока
- std::vector<uint8_t> tempBlock(4);
- uint32_t sourceBlock = 0;
- uint32_t resultBlock = 0;
- uint32_t key = feistelKey_;
- for ( size_t i = 0; i < sourceText.size(); i+=4 )
- {
- // 4 байта открытого текста
- tempBlock[0] = sourceText[i];
- tempBlock[1] = sourceText[i + 1];
- tempBlock[2] = sourceText[i + 2];
- tempBlock[3] = sourceText[i + 3];
- // Упакуем в uint32_t
- sourceBlock = FormBlock( tempBlock );
- // Шифруем
- resultBlock = EncryptBlock( sourceBlock, key ^ sourceBlock );
- // Записываем результат
- std::vector<uint8_t> deformedBlock = DeformBlock( resultBlock );
- key = resultBlock;
- }
- sourceBlock = sourceSize;
- resultBlock = EncryptBlock( sourceBlock, key ^ sourceBlock );
- return resultBlock;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement