Deszyfracja została skończona i działa poprawnie

This commit is contained in:
yanczi 2025-11-04 03:53:08 +01:00
parent c234825ac5
commit 55c273eace
8 changed files with 104 additions and 20 deletions

4
.gitignore vendored
View file

@ -368,3 +368,7 @@ test2/
*.pak *.pak
expak/ expak/
x64/ x64/
test.*
pest2.*
*.hh
*.key

View file

@ -49,6 +49,11 @@
#define ALL_FILE ".*" // Wszystkie pliki #define ALL_FILE ".*" // Wszystkie pliki
struct MethodFlags
{
bool compressing = false;
bool encryption = false;
};
class CreateCargo { class CreateCargo {
public: public:
@ -65,6 +70,8 @@ private:
const std::string extension; const std::string extension;
const uint8_t version; const uint8_t version;
MethodFlags methodFlags;
std::string catalogPath; std::string catalogPath;
std::string tempFile; std::string tempFile;

View file

@ -21,9 +21,8 @@ std::vector<char> EncryptionManager::encrypt(const std::vector<char>& raw)
reinterpret_cast<unsigned char*>(crypt.data()), reinterpret_cast<unsigned char*>(crypt.data()),
reinterpret_cast<const unsigned char*>(raw.data()), reinterpret_cast<const unsigned char*>(raw.data()),
static_cast<unsigned long long>(raw.size()), static_cast<unsigned long long>(raw.size()),
nonce.data(), nonce.data(), 0, key.data()) != 0)
0, {
key.data()) != 0) {
throw std::runtime_error("crypto_stream_chacha20_ietf_xor_ic failed"); 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 // Wygeneruj time stamp
std::time_t now = std::time(nullptr); std::time_t now = std::time(nullptr);
const uint32_t time = static_cast<uint32_t>(now); const int time = static_cast<int>(now);
// Wygeneruj crc kluczy // Wygeneruj crc kluczy
const uint64_t crcKey = XXH64(key.data(), key.size(), 0); std::vector<char> keyVec(reinterpret_cast<const char*>(key.data()),
const uint64_t crcNonce = XXH64(nonce.data(), nonce.size(), 0); reinterpret_cast<const char*>(key.data()) + key.size());
std::vector<char> nonceVec(reinterpret_cast<const char*>(nonce.data()),
reinterpret_cast<const char*>(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 // Zapisz ten œmietnik do pliku KEY
std::ofstream file(path + ".key", std::ios::binary); std::ofstream file(path + ".key", std::ios::binary);
@ -65,16 +70,16 @@ void EncryptionManager::saveKey(const std::string& path)
file.write(reinterpret_cast<const char*>(&sig), sizeof(sig)); file.write(reinterpret_cast<const char*>(&sig), sizeof(sig));
file.write(reinterpret_cast<const char*>(&ver), sizeof(ver)); file.write(reinterpret_cast<const char*>(&ver), sizeof(ver));
file.write(reinterpret_cast<const char*>(&time), sizeof(time)); file.write(reinterpret_cast<const char*>(&time), sizeof(time));
file.write(reinterpret_cast<const char*>(key.data()), key.size()); file.write(reinterpret_cast<const char*>(keyVec.data()), keyVec.size());
file.write(reinterpret_cast<const char*>(&crcKey), sizeof(crcKey)); file.write(reinterpret_cast<const char*>(&crcKey), sizeof(crcKey));
file.write(reinterpret_cast<const char*>(nonce.data()), nonce.size()); file.write(reinterpret_cast<const char*>(nonceVec.data()), nonceVec.size());
file.write(reinterpret_cast<const char*>(&crcNonce), sizeof(crcNonce)); file.write(reinterpret_cast<const char*>(&crcNonce), sizeof(crcNonce));
if (!file.good()) { std::cout << "Dupa nie zapisa³o" << std::endl; } if (!file.good()) { std::cout << "Dupa nie zapisa³o" << std::endl; }
file.close(); file.close();
saveCppHeadFile(path); //saveCppHeadFile(path);
} }
// Generowanie pliku nag³ówkowego CPP z kluczem i nonce // Generowanie pliku nag³ówkowego CPP z kluczem i nonce
@ -90,7 +95,7 @@ void EncryptionManager::saveCppHeadFile(const std::string& path)
"// Ci¹g nonce\n" "// Ci¹g nonce\n"
"const std::array<unsigned char, crypto_stream_chacha20_ietf_NONCEBYTES> nonce{" + toHex(nonceVec) + "}; "; "const std::array<unsigned char, crypto_stream_chacha20_ietf_NONCEBYTES> nonce{" + toHex(nonceVec) + "}; ";
std::ofstream file(path + ".hpp"); std::ofstream file(path + ".hh");
file << headerText; file << headerText;
file.close(); file.close();
} }
@ -124,5 +129,63 @@ std::string EncryptionManager::toHex(const std::vector<unsigned char>& data)
// Wczytaj klucz // Wczytaj klucz
void EncryptionManager::loadKey(const std::string& path) 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<char*>(&sig), sizeof(sig));
file.read(reinterpret_cast<char*>(&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<char> keyVec(key.size());
std::vector<char> nonceVec(nonce.size());
uint64_t crcKey;
uint64_t crcNonce;
file.read(reinterpret_cast<char*>(&time), sizeof(time));
file.read(keyVec.data(), keyVec.size());
file.read(reinterpret_cast<char*>(&crcKey), sizeof(crcKey));
file.read(nonceVec.data(), nonceVec.size());
file.read(reinterpret_cast<char*>(&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<crypto_stream_chacha20_ietf_KEYBYTES>(keyVec);
nonce = toArray<crypto_stream_chacha20_ietf_NONCEBYTES>(nonceVec);
}
// Deszyfracja
std::vector<char> EncryptionManager::decrypt(const std::vector<char>& crypt)
{
std::vector<char> raw(crypt.size());
if (crypto_stream_chacha20_ietf_xor(
reinterpret_cast<unsigned char*>(raw.data()),
reinterpret_cast<const unsigned char*>(crypt.data()),
static_cast<unsigned long long>(crypt.size()),
nonce.data(), key.data()) != 0)
{
throw std::runtime_error("Data decryption error!");
}
return raw;
} }

View file

@ -21,7 +21,7 @@ public:
~EncryptionManager() = default; ~EncryptionManager() = default;
std::vector<char> encrypt(const std::vector<char>&); std::vector<char> encrypt(const std::vector<char>&);
//std::vector<char> decrypt(const std::vector<char>&); std::vector<char> decrypt(const std::vector<char>&);
void saveKey(const std::string&); void saveKey(const std::string&);
void loadKey(const std::string&); void loadKey(const std::string&);
@ -34,4 +34,15 @@ private:
void generateKeys(); void generateKeys();
std::string toHex(const std::vector<unsigned char>&); std::string toHex(const std::vector<unsigned char>&);
void saveCppHeadFile(const std::string&); void saveCppHeadFile(const std::string&);
template <size_t N>
std::array<unsigned char, N> toArray(const std::vector<char>& vec) {
if (vec.size() < N) {
throw std::runtime_error("Too small vector to convert to array");
}
std::array<unsigned char, N> arr{};
std::memcpy(arr.data(), vec.data(), N);
return arr;
}
}; };

View file

@ -59,6 +59,10 @@ bool ExtractCargo::Extract(const std::string& cFile)
return false; return false;
} }
// Wczytaj klucz deszyfruj¹cy
std::filesystem::path kdir = cargoFileName.stem();
eman.loadKey(kdir.string());
//Otwieranie kontenera //Otwieranie kontenera
cargoFile.open(cargoFileName, std::ios::binary); cargoFile.open(cargoFileName, std::ios::binary);
@ -170,7 +174,7 @@ void ExtractCargo::ExtractingFilesFromCargo()
cargoFile.read(buffor.data(), fh.size); cargoFile.read(buffor.data(), fh.size);
std::vector<char> rawBuffor = fh.isZip ? cm.decompress(buffor) : buffor; std::vector<char> rawBuffor = fh.isZip ? cm.decompress(buffor) : eman.decrypt(buffor);
if (!HashValid(rawBuffor, fh.crc)) if (!HashValid(rawBuffor, fh.crc))
{ {

View file

@ -32,6 +32,7 @@
#include "DataStruct.h" #include "DataStruct.h"
#include "CompressingManager.h" #include "CompressingManager.h"
#include "EncryptionManager.h"
class ExtractCargo { class ExtractCargo {
public: public:
@ -56,6 +57,7 @@ private:
std::ifstream cargoFile; std::ifstream cargoFile;
EncryptionManager eman;
// Sprawdzenie poprawności archiwum // Sprawdzenie poprawności archiwum

View file

@ -1,7 +0,0 @@
// Plik wygenerowany przy wykorzystaniu exPAK
// Klucz deszyfruj¹cy
const std::array<unsigned char, crypto_stream_chacha20_ietf_KEYBYTES> 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<unsigned char, crypto_stream_chacha20_ietf_NONCEBYTES> nonce{'0x5E', '0x8F', '0x8B', '0x17', '0x16', '0xE3', '0xDB', '0xE0', '0xBF', '0xF0', '0x25', '0xDA'};

BIN
test.key

Binary file not shown.