#include "EncryptionManager.h" EncryptionManager::EncryptionManager() :keyReady(false) { if (sodium_init() < 0) { throw std::runtime_error("libsodium init failed"); } keyReady = false; } std::vector EncryptionManager::encrypt(const std::vector& raw) { std::vector crypt(raw.size()); // Generowanie kluczy generateKeys(); if (crypto_stream_chacha20_ietf_xor_ic( reinterpret_cast(crypt.data()), reinterpret_cast(raw.data()), static_cast(raw.size()), nonce.data(), 0, key.data()) != 0) { throw std::runtime_error("crypto_stream_chacha20_ietf_xor_ic failed"); } return crypt; } void EncryptionManager::generateKeys() { if (keyReady) return; std::cout << "GENEROWANIE KLUCZA" << std::endl; //randombytes_buf(key.data(), key.size()); crypto_stream_chacha20_ietf_keygen(key.data()); randombytes_buf(nonce.data(), nonce.size()); keyReady = true; } void EncryptionManager::saveKey(const std::string& path) { std::cout << "ZAPISYWANIE KLUCZA" << std::endl; const int sig = SIGNATURE_KEY_FILE; const short ver = VERSION; // Wygeneruj time stamp std::time_t now = std::time(nullptr); const uint32_t 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); // Zapisz ten śmietnik do pliku KEY std::ofstream file(path + ".key", std::ios::binary); if (!file) { std::cout << "Dupa nie zapisało" << std::endl; } 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(&crcKey), sizeof(crcKey)); file.write(reinterpret_cast(nonce.data()), nonce.size()); file.write(reinterpret_cast(&crcNonce), sizeof(crcNonce)); if (!file.good()) { std::cout << "Dupa nie zapisało" << std::endl; } file.close(); saveCppHeadFile(path); } // Generowanie pliku nagłówkowego CPP z kluczem i nonce void EncryptionManager::saveCppHeadFile(const std::string& path) { const std::string headerText = "// Plik wygenerowany przy wykorzystaniu exPAK\n\n" "// Klucz deszyfrujący\n" "const std::array key{};\n\n" "// Ciąg nonce\n" "const std::array nonce{};"; std::ofstream file(path + ".hpp"); file << headerText; file.close(); } // Wczytaj klucz void EncryptionManager::loadKey(const std::string& path) { }