From 855f079bf733f674bfd77368dff4bf82ba7057f9 Mon Sep 17 00:00:00 2001 From: yanczi Date: Wed, 19 Nov 2025 20:07:59 +0100 Subject: [PATCH] =?UTF-8?q?Nonce=20jest=20cz=C4=99=C5=9Bci=C4=85=20chunka?= =?UTF-8?q?=20danych?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EncryptionManager.cpp | 64 ++++++++++++++++++++----------------------- EncryptionManager.h | 1 - 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/EncryptionManager.cpp b/EncryptionManager.cpp index 763b711..aa49bd8 100644 --- a/EncryptionManager.cpp +++ b/EncryptionManager.cpp @@ -13,21 +13,27 @@ EncryptionManager::EncryptionManager() std::vector EncryptionManager::encrypt(const std::vector& raw) { - std::vector crypt(raw.size()); - - // Generowanie kluczy - // generateKeys(); + std::array nonce_local; + randombytes_buf(nonce_local.data(), nonce_local.size()); + std::vector tmp(raw.size()); if (crypto_stream_chacha20_ietf_xor_ic( - reinterpret_cast(crypt.data()), + reinterpret_cast(tmp.data()), reinterpret_cast(raw.data()), - static_cast(raw.size()), - nonce.data(), 0, key.data()) != 0) + static_cast(raw.size()), + nonce_local.data(), 0, key.data()) != 0) { throw std::runtime_error("crypto_stream_chacha20_ietf_xor_ic failed"); } - return crypt; + std::vector output; + output.insert(output.end(), + reinterpret_cast(nonce_local.data()), + reinterpret_cast(nonce_local.data()) + nonce_local.size()); + + output.insert(output.end(), tmp.begin(), tmp.end()); + + return output; } void EncryptionManager::generateKeys() @@ -36,7 +42,6 @@ void EncryptionManager::generateKeys() //randombytes_buf(key.data(), key.size()); crypto_stream_chacha20_ietf_keygen(key.data()); - randombytes_buf(nonce.data(), nonce.size()); keyReady = true; } @@ -54,11 +59,7 @@ void EncryptionManager::saveKey(const std::string& path, bool hpp) 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); @@ -69,8 +70,6 @@ void EncryptionManager::saveKey(const std::string& path, bool hpp) file.write(reinterpret_cast(&time), sizeof(time)); file.write(reinterpret_cast(keyVec.data()), keyVec.size()); file.write(reinterpret_cast(&crcKey), sizeof(crcKey)); - file.write(reinterpret_cast(nonceVec.data()), nonceVec.size()); - file.write(reinterpret_cast(&crcNonce), sizeof(crcNonce)); file.close(); @@ -80,11 +79,7 @@ void EncryptionManager::saveKey(const std::string& path, bool hpp) // Generowanie pliku nagłówkowego CPP z kluczem i nonce void EncryptionManager::saveCppHeadFile(const std::string& path) { - std::vector keyVec(key.begin(), key.end()); - std::vector nonceVec(nonce.begin(), nonce.end()); - const uint32_t keySize = crypto_stream_chacha20_ietf_KEYBYTES; - const uint32_t nonceSize = crypto_stream_chacha20_ietf_NONCEBYTES; std::ofstream file(path + ".hpp"); @@ -102,10 +97,6 @@ void EncryptionManager::saveCppHeadFile(const std::string& path) file << " " << toHex(key.data(), key.size()) << std::endl; file << " };" << std::endl; file << std::endl; - file << " // Ciąg nonce" << std::endl; - file << " const std::array nonce{" << std::endl; - file << " " << toHex(nonce.data(), nonce.size()) << std::endl; - file << " }; " << std::endl; file << "} //namespace" << std::endl; file.close(); @@ -143,19 +134,14 @@ void EncryptionManager::loadKey(const std::string& path) } 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)); // Sprawdź integralność klucza - if (XXH64(keyVec.data(), keyVec.size(), VERSION) != crcKey - || XXH64(nonceVec.data(), nonceVec.size(), VERSION) != crcNonce) + if (XXH64(keyVec.data(), keyVec.size(), VERSION) != crcKey) { throw std::runtime_error("Key integrity error!"); } @@ -164,19 +150,29 @@ void EncryptionManager::loadKey(const std::string& path) // Przekonwertuj vector na array key = toArray(keyVec); - nonce = toArray(nonceVec); } // Deszyfracja std::vector EncryptionManager::decrypt(const std::vector& crypt) { - std::vector raw(crypt.size()); + const size_t cryptoSize = crypto_stream_chacha20_ietf_NONCEBYTES; + + std::array nonce_local; + std::memcpy(nonce_local.data(), + reinterpret_cast(crypt.data()), cryptoSize); + + + const size_t rawSize = crypt.size() - cryptoSize; + std::vector tmp(rawSize); + std::memcpy(tmp.data(), crypt.data() + cryptoSize, rawSize); + + std::vector raw(rawSize); if (crypto_stream_chacha20_ietf_xor( reinterpret_cast(raw.data()), - reinterpret_cast(crypt.data()), - static_cast(crypt.size()), - nonce.data(), key.data()) != 0) + reinterpret_cast(tmp.data()), + static_cast(tmp.size()), + nonce_local.data(), key.data()) != 0) { throw std::runtime_error("Data decryption error!"); } diff --git a/EncryptionManager.h b/EncryptionManager.h index 601b8ac..61a03e4 100644 --- a/EncryptionManager.h +++ b/EncryptionManager.h @@ -28,7 +28,6 @@ public: private: std::array key{}; - std::array nonce{}; bool keyReady; void generateKeys();