Działa szyfrowanie, generowanie klucza i zapisywanie klucza do pliku KEY
This commit is contained in:
parent
b80d983bc7
commit
8745ed2e19
7 changed files with 154 additions and 3 deletions
|
|
@ -1,18 +1,21 @@
|
|||
#include "EncryptionManager.h"
|
||||
|
||||
EncryptionManager::EncryptionManager()
|
||||
:keyReady(false)
|
||||
{
|
||||
if (sodium_init() < 0) {
|
||||
throw std::runtime_error("libsodium init failed");
|
||||
}
|
||||
|
||||
keyReady = false;
|
||||
}
|
||||
|
||||
std::vector<char> EncryptionManager::encrypt(const std::vector<char>& raw)
|
||||
{
|
||||
randombytes_buf(key.data(), key.size());
|
||||
randombytes_buf(nonce.data(), nonce.size());
|
||||
|
||||
std::vector<char> crypt(raw.size());
|
||||
|
||||
// Generowanie kluczy
|
||||
generateKeys();
|
||||
|
||||
if (crypto_stream_chacha20_ietf_xor_ic(
|
||||
reinterpret_cast<unsigned char*>(crypt.data()),
|
||||
|
|
@ -25,4 +28,67 @@ std::vector<char> EncryptionManager::encrypt(const std::vector<char>& raw)
|
|||
}
|
||||
|
||||
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<uint32_t>(now);
|
||||
|
||||
// Przekonwertuj array z kluczem i nonce na vector char
|
||||
std::vector<char> keyVec(reinterpret_cast<const char*>(key.data()), 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());
|
||||
|
||||
// Wygeneruj crc kluczy
|
||||
const uint16_t crcKey = crc16(keyVec);
|
||||
const uint16_t crcNonce = crc16(nonceVec);
|
||||
|
||||
// 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<const char*>(&sig), sizeof(sig));
|
||||
file.write(reinterpret_cast<const char*>(&ver), sizeof(ver));
|
||||
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*>(&crcKey), sizeof(crcKey));
|
||||
file.write(reinterpret_cast<const char*>(nonce.data()), nonce.size());
|
||||
file.write(reinterpret_cast<const char*>(&crcNonce), sizeof(crcNonce));
|
||||
|
||||
if (!file.good()) { std::cout << "Dupa nie zapisa³o" << std::endl; }
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
// Wczytaj klucz
|
||||
void EncryptionManager::loadKey(const std::string& path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Wygeneruj CRC16
|
||||
uint16_t EncryptionManager::crc16(const std::vector<char>& buffer)
|
||||
{
|
||||
boost::crc_16_type crc;
|
||||
crc.process_bytes(buffer.data(), buffer.size());
|
||||
return crc.checksum();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue