#include "coder.hpp" namespace Coder { typedef uint8_t vect[4]; thread_local uint8_t key[32] = { 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff }; //Ключ шифрования thread_local unsigned char Pi[8][16]= { {1,7,14,13,0,5,8,3,4,15,10,6,9,12,11,2}, {8,14,2,5,6,9,1,12,15,4,11,0,13,10,3,7}, {5,13,15,6,9,2,12,10,11,7,8,1,4,3,14,0}, {7,15,5,10,8,1,6,13,0,9,3,14,11,4,2,12}, {12,8,2,1,13,4,15,6,7,0,10,5,3,14,9,11}, {11,3,5,8,2,15,10,13,14,1,7,4,12,9,6,0}, {6,8,2,3,9,10,5,12,1,14,4,7,11,13,0,15}, {12,4,6,2,10,5,11,9,14,8,13,7,0,3,15,1} }; //Таблица подстановок void Transform_T(const uint8_t *in_data, uint8_t *out_data) { uint8_t first_part_byte, sec_part_byte; int i; for (i = 0; i < 4; i++) { // Извлекаем первую 4-битную часть байта first_part_byte = (in_data[i] & 0xf0) >> 4; // Извлекаем вторую 4-битную часть байта sec_part_byte = (in_data[i] & 0x0f); // Выполняем замену в соответствии с таблицей подстановок first_part_byte = Pi[i * 2][first_part_byte]; sec_part_byte = Pi[i * 2 + 1][sec_part_byte]; // «Склеиваем» обе 4-битные части обратно в байт out_data[i] = (first_part_byte << 4) | sec_part_byte; } } //Сложение двух двоичных векторов по модулю 2 void Sum_ABS_2(const uint8_t *a, const uint8_t *b, uint8_t *c) { int i; for (i = 0; i < 4; i++) c[i] = a[i]^b[i]; } //Сложение двух двоичных векторов по модулю 32 void Sum_ABS_32(const uint8_t *a, const uint8_t *b, uint8_t *c) { int i; unsigned int internal = 0; for (i = 3; i >= 0; i--) { internal = a[i] + b[i] + (internal >> 8); c[i] = internal & 0xff; } } //Развертывание ключа void Expand_Key(vect* iter_key) { for(uint8_t i=0; i<8; i++) { memcpy(iter_key[i], key + 4*i, 4); memcpy(iter_key[i+8], key + 4*i, 4); memcpy(iter_key[i+16], key + 4*i, 4); memcpy(iter_key[i+24], key + 4*(7-i), 4); } } void Transform_g(const uint8_t *k, const uint8_t *a, uint8_t *out_data) { uint8_t internal[4]; uint32_t out_data_32; // Складываем по модулю 32 правую половину блока с итерационным ключом Sum_ABS_32(a, k, internal); // Производим нелинейное биективное преобразование результата Transform_T(internal, internal); // Преобразовываем четырехбайтный вектор в одно 32-битное число out_data_32 = internal[0]; out_data_32 = (out_data_32 << 8) + internal[1]; out_data_32 = (out_data_32 << 8) + internal[2]; out_data_32 = (out_data_32 << 8) + internal[3]; // Циклически сдвигаем все влево на 11 разрядов out_data_32 = (out_data_32 << 11)|(out_data_32 >> 21); // Преобразовываем 32-битный результат сдвига обратно в 4-байтовый вектор out_data[3] = out_data_32; out_data[2] = out_data_32 >> 8; out_data[1] = out_data_32 >> 16; out_data[0] = out_data_32 >> 24; } void Transform_G(const uint8_t *k, const uint8_t *a, uint8_t *out_data) { uint8_t a_0[4]; // Правая половина блока uint8_t a_1[4]; // Левая половина блока uint8_t G[4]; int i; // Делим 64-битный исходный блок на две части for(i = 0; i < 4; i++) { a_0[i] = a[4 + i]; a_1[i] = a[i]; } // Производим преобразование g Transform_g(k, a_0, G); // Ксорим результат преобразования g с левой половиной блока Sum_ABS_2(a_1, G, G); for(i = 0; i < 4; i++) { a_1[i] = a_0[i]; a_0[i] = G[i]; } // Сводим правую и левую части блока в одно целое for(i = 0; i < 4; i++) { out_data[i] = a_1[i]; out_data[4 + i] = a_0[i]; } } void Transform_G_Fin(const uint8_t *k, const uint8_t *a, uint8_t *out_data) { uint8_t a_0[4]; // Правая половина блока uint8_t a_1[4]; // Левая половина блока uint8_t G[4]; int i; // Делим 64-битный исходный блок на две части for(i = 0; i < 4; i++) { a_0[i] = a[4 + i]; a_1[i] = a[i]; } // Производим преобразование g Transform_g(k, a_0, G); // Ксорим результат преобразования g с левой половиной блока Sum_ABS_2(a_1, G, G); for(i = 0; i < 4; i++) a_1[i] = G[i]; for(i = 0; i < 4; i++) { out_data[i] = a_1[i]; out_data[4 + i] = a_0[i]; } } void Encrypt(const uint8_t *blk, uint8_t *out_blk, vect* iter_key) { int i; Transform_G(iter_key[0], blk, out_blk); for(i = 1; i < 31; i++) Transform_G(iter_key[i], out_blk, out_blk); Transform_G_Fin(iter_key[31], out_blk, out_blk); } void Decrypt(const uint8_t *blk, uint8_t *out_blk, vect* iter_key) { int i; Transform_G(iter_key[31], blk, out_blk); for(i = 30; i > 0; i--) Transform_G(iter_key[i], out_blk, out_blk); Transform_G_Fin(iter_key[0], out_blk, out_blk); } std::string Encode(std::string str) { vect iter_key[32]; Expand_Key(iter_key); size_t i = 0; while(i + 7 < str.length()) { uint8_t block[8]; for(size_t j = 0; j < 8; j++) { block[j] = static_cast(str[i+j]); } Encrypt(block, block, iter_key); for(size_t j = 0; j < 8; j++) { str[i+j] = static_cast(block[j]); } i = i + 8; } if(i < str.length()) { uint8_t block[8]; size_t j = 0; while(j + i < str.length()) { block[j] = static_cast(str[i+j]); j++; } while(j < 8) { block[j] = static_cast(' '); j++; } Encrypt(block, block, iter_key); str.resize(i+8); for(size_t j = 0; j < 8; j++) { str[i+j] = static_cast(block[j]); } } return str; } uint8_t CharToUint8_t(char& ch) { if((ch>='0')&&(ch<='9')) { return static_cast(ch - '0'); } else { return static_cast(10 + ch - 'a'); } } std::string HexToStr(std::string& hexstr) { std::string str(hexstr.length()/2, ' '); size_t j=0; for(size_t i=0; i(ch1 + ch2); } return str; } std::string Decode(std::string str) { vect iter_key[32]; Expand_Key(iter_key); size_t i = 0; while(i + 7 < str.length()) { uint8_t block[8]; for(size_t j = 0; j < 8; j++) { block[j] = static_cast(str[i+j]); } Decrypt(block, block, iter_key); for(size_t j = 0; j < 8; j++) { str[i+j] = static_cast(block[j]); } i = i + 8; } if(i > 0){ while(str[--i] == ' ') {} str.resize(++i); } return str; } bool CheckHex(const std::string& str) { if(str.length()%16!=0) { return false; } else { for(char ch : str) { if(!(((ch>='0')&&(ch<='9'))||((ch>='a')&&(ch<='f')))) return false; } } return true; } std::string str_to_hex(const unsigned char* data, size_t length) { std::ostringstream oss; for (size_t i = 0; i < length; ++i) { oss << std::hex << std::setw(2) << std::setfill('0') << static_cast(data[i]); } return oss.str(); } std::string hex_to_str(const std::string& hex) { std::vector bytes; size_t length = hex.length(); // Проверяем, что длина строки четная if (length % 2 != 0) { throw std::invalid_argument("Hex string must have an even length"); } for (size_t i = 0; i < length; i += 2) { std::string byteString = hex.substr(i, 2); unsigned char byte = static_cast(std::stoi(byteString, nullptr, 16)); bytes.push_back(byte); } std::ostringstream result; for (const auto& b : bytes) { result << b; } return result.str(); } std::string decode_pwd(std::string pwd) { if (pwd.rfind(HASH_KWD) != 0) return pwd; pwd = pwd.substr(strlen(HASH_KWD)); pwd = Decode(hex_to_str(pwd)).substr(strlen(pPASSWORDSALT)); return pwd; } std::string encode_pwd(std::string pwd) { if (pwd.rfind(HASH_KWD) == 0) return pwd; auto hased_pwd = Encode(pPASSWORDSALT + pwd); pwd = HASH_KWD + str_to_hex(reinterpret_cast(hased_pwd.c_str()), hased_pwd.size()); return pwd; } }