From 55c273eaceb30d3205b88863a7c200d118445987 Mon Sep 17 00:00:00 2001 From: yanczi Date: Tue, 4 Nov 2025 03:53:08 +0100 Subject: [PATCH] =?UTF-8?q?Deszyfracja=20zosta=C5=82a=20sko=C5=84czona=20i?= =?UTF-8?q?=20dzia=C5=82a=20poprawnie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 ++- CreateCargo.h | 7 ++++ EncryptionManager.cpp | 83 +++++++++++++++++++++++++++++++++++++----- EncryptionManager.h | 13 ++++++- ExtractCargo.cpp | 6 ++- ExtractCargo.h | 2 + test.hpp | 7 ---- test.key | Bin 70 -> 0 bytes 8 files changed, 104 insertions(+), 20 deletions(-) delete mode 100644 test.hpp delete mode 100644 test.key diff --git a/.gitignore b/.gitignore index 92b3625..1b9f212 100644 --- a/.gitignore +++ b/.gitignore @@ -367,4 +367,8 @@ test/ test2/ *.pak expak/ -x64/ \ No newline at end of file +x64/ +test.* +pest2.* +*.hh +*.key \ No newline at end of file diff --git a/CreateCargo.h b/CreateCargo.h index 9c2090b..12a9c36 100644 --- a/CreateCargo.h +++ b/CreateCargo.h @@ -49,6 +49,11 @@ #define ALL_FILE ".*" // Wszystkie pliki +struct MethodFlags +{ + bool compressing = false; + bool encryption = false; +}; class CreateCargo { public: @@ -65,6 +70,8 @@ private: const std::string extension; const uint8_t version; + MethodFlags methodFlags; + std::string catalogPath; std::string tempFile; diff --git a/EncryptionManager.cpp b/EncryptionManager.cpp index 98cb0da..ed67797 100644 --- a/EncryptionManager.cpp +++ b/EncryptionManager.cpp @@ -21,9 +21,8 @@ std::vector EncryptionManager::encrypt(const std::vector& raw) reinterpret_cast(crypt.data()), reinterpret_cast(raw.data()), static_cast(raw.size()), - nonce.data(), - 0, - key.data()) != 0) { + nonce.data(), 0, key.data()) != 0) + { throw std::runtime_error("crypto_stream_chacha20_ietf_xor_ic failed"); } @@ -52,11 +51,17 @@ void EncryptionManager::saveKey(const std::string& path) // Wygeneruj time stamp std::time_t now = std::time(nullptr); - const uint32_t time = static_cast(now); + const int time = static_cast(now); // Wygeneruj crc kluczy - const uint64_t crcKey = XXH64(key.data(), key.size(), 0); - const uint64_t crcNonce = XXH64(nonce.data(), nonce.size(), 0); + std::vector keyVec(reinterpret_cast(key.data()), + reinterpret_cast(key.data()) + key.size()); + + std::vector nonceVec(reinterpret_cast(nonce.data()), + reinterpret_cast(nonce.data()) + nonce.size()); + + const uint64_t crcKey = XXH64(keyVec.data(), keyVec.size(), VERSION); + const uint64_t crcNonce = XXH64(nonceVec.data(), nonceVec.size(), VERSION); // Zapisz ten śmietnik do pliku KEY std::ofstream file(path + ".key", std::ios::binary); @@ -65,16 +70,16 @@ void EncryptionManager::saveKey(const std::string& path) file.write(reinterpret_cast(&sig), sizeof(sig)); file.write(reinterpret_cast(&ver), sizeof(ver)); file.write(reinterpret_cast(&time), sizeof(time)); - file.write(reinterpret_cast(key.data()), key.size()); + file.write(reinterpret_cast(keyVec.data()), keyVec.size()); file.write(reinterpret_cast(&crcKey), sizeof(crcKey)); - file.write(reinterpret_cast(nonce.data()), nonce.size()); + file.write(reinterpret_cast(nonceVec.data()), nonceVec.size()); file.write(reinterpret_cast(&crcNonce), sizeof(crcNonce)); if (!file.good()) { std::cout << "Dupa nie zapisało" << std::endl; } file.close(); - saveCppHeadFile(path); + //saveCppHeadFile(path); } // Generowanie pliku nagłówkowego CPP z kluczem i nonce @@ -90,7 +95,7 @@ void EncryptionManager::saveCppHeadFile(const std::string& path) "// Ciąg nonce\n" "const std::array nonce{" + toHex(nonceVec) + "}; "; - std::ofstream file(path + ".hpp"); + std::ofstream file(path + ".hh"); file << headerText; file.close(); } @@ -124,5 +129,63 @@ std::string EncryptionManager::toHex(const std::vector& data) // Wczytaj klucz void EncryptionManager::loadKey(const std::string& path) { + std::cout << "ODCZYT KLUCZA" << std::endl; + std::ifstream file(path + ".key", std::ios::binary); + + int sig; + short ver; + int time; + + // Wczytaj + file.read(reinterpret_cast(&sig), sizeof(sig)); + file.read(reinterpret_cast(&ver), sizeof(ver)); + + // Sprawdź czy plik klucza jest poprawny + if (sig != SIGNATURE_KEY_FILE || ver != VERSION) + { + throw std::runtime_error("Invalid key file!"); + } + + std::vector keyVec(key.size()); + std::vector nonceVec(nonce.size()); + uint64_t crcKey; + uint64_t crcNonce; + + file.read(reinterpret_cast(&time), sizeof(time)); + file.read(keyVec.data(), keyVec.size()); + file.read(reinterpret_cast(&crcKey), sizeof(crcKey)); + file.read(nonceVec.data(), nonceVec.size()); + file.read(reinterpret_cast(&crcNonce), sizeof(crcNonce)); + + std::cout << crcKey << " - " << XXH64(keyVec.data(), keyVec.size(), VERSION) << std::endl; + + // Sprawdź integralność klucza + if (XXH64(keyVec.data(), keyVec.size(), VERSION) != crcKey + || XXH64(nonceVec.data(), nonceVec.size(), VERSION) != crcNonce) + { + throw std::runtime_error("Key integrity error!"); + } + + file.close(); + // Przekonwertuj vector na array + key = toArray(keyVec); + nonce = toArray(nonceVec); +} + +// Deszyfracja +std::vector EncryptionManager::decrypt(const std::vector& crypt) +{ + std::vector raw(crypt.size()); + + if (crypto_stream_chacha20_ietf_xor( + reinterpret_cast(raw.data()), + reinterpret_cast(crypt.data()), + static_cast(crypt.size()), + nonce.data(), key.data()) != 0) + { + throw std::runtime_error("Data decryption error!"); + } + + return raw; } \ No newline at end of file diff --git a/EncryptionManager.h b/EncryptionManager.h index fad298d..bee6845 100644 --- a/EncryptionManager.h +++ b/EncryptionManager.h @@ -21,7 +21,7 @@ public: ~EncryptionManager() = default; std::vector encrypt(const std::vector&); - //std::vector decrypt(const std::vector&); + std::vector decrypt(const std::vector&); void saveKey(const std::string&); void loadKey(const std::string&); @@ -34,4 +34,15 @@ private: void generateKeys(); std::string toHex(const std::vector&); void saveCppHeadFile(const std::string&); + + template + std::array toArray(const std::vector& vec) { + if (vec.size() < N) { + throw std::runtime_error("Too small vector to convert to array"); + } + + std::array arr{}; + std::memcpy(arr.data(), vec.data(), N); + return arr; + } }; \ No newline at end of file diff --git a/ExtractCargo.cpp b/ExtractCargo.cpp index bf5e028..23edcd3 100644 --- a/ExtractCargo.cpp +++ b/ExtractCargo.cpp @@ -59,6 +59,10 @@ bool ExtractCargo::Extract(const std::string& cFile) return false; } + // Wczytaj klucz deszyfrujący + std::filesystem::path kdir = cargoFileName.stem(); + eman.loadKey(kdir.string()); + //Otwieranie kontenera cargoFile.open(cargoFileName, std::ios::binary); @@ -170,7 +174,7 @@ void ExtractCargo::ExtractingFilesFromCargo() cargoFile.read(buffor.data(), fh.size); - std::vector rawBuffor = fh.isZip ? cm.decompress(buffor) : buffor; + std::vector rawBuffor = fh.isZip ? cm.decompress(buffor) : eman.decrypt(buffor); if (!HashValid(rawBuffor, fh.crc)) { diff --git a/ExtractCargo.h b/ExtractCargo.h index e7d89b3..164a0fc 100644 --- a/ExtractCargo.h +++ b/ExtractCargo.h @@ -32,6 +32,7 @@ #include "DataStruct.h" #include "CompressingManager.h" +#include "EncryptionManager.h" class ExtractCargo { public: @@ -56,6 +57,7 @@ private: std::ifstream cargoFile; + EncryptionManager eman; // Sprawdzenie poprawności archiwum diff --git a/test.hpp b/test.hpp deleted file mode 100644 index 0ac6d06..0000000 --- a/test.hpp +++ /dev/null @@ -1,7 +0,0 @@ -// Plik wygenerowany przy wykorzystaniu exPAK - -// Klucz deszyfrujący -const std::array key{'0xBB', '0xEF', '0x33', '0xD0', '0x9F', '0xB2', '0xAB', '0x54', '0xD3', '0xF2', '0xDA', '0xF7', '0x51', '0x5B', '0x67', '0x8C', '0xA4', '0xAA', '0xF1', '0xC0', '0x14', '0x81', '0x8C', '0x23', '0x86', '0x06', '0xBC', '0x1E', '0xE6', '0x49', '0xF1', '0xE0'}; - -// Ciąg nonce -const std::array nonce{'0x5E', '0x8F', '0x8B', '0x17', '0x16', '0xE3', '0xDB', '0xE0', '0xBF', '0xF0', '0x25', '0xDA'}; \ No newline at end of file diff --git a/test.key b/test.key deleted file mode 100644 index 552c17133dbe59b800041cbfe5850964f5cde34b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 70 zcmV-M0J;BIOGQ~^0EN5;X}j+;(4VraRMYa>_fcDCjHIgZz!ZUuBZdaN9_C5$;Fm;1 cu5o}I4_=Rp7Z&5&;J@%C+C10~?PSSUed~@S8~^|S