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
|
|
@ -175,6 +175,7 @@ uint8_t CreateCargo::CheckFileOnTheList(const std::string& path, std::vector<cha
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
output = std::move(input);
|
output = std::move(input);
|
||||||
|
//output = crypt.encrypt(input);
|
||||||
return RAW_FILE;
|
return RAW_FILE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -188,6 +189,7 @@ uint8_t CreateCargo::CheckFileOnTheList(const std::string& path, std::vector<cha
|
||||||
}
|
}
|
||||||
|
|
||||||
output = std::move(input);
|
output = std::move(input);
|
||||||
|
//output = crypt.encrypt(input);
|
||||||
return RAW_FILE;
|
return RAW_FILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -451,6 +453,9 @@ bool CreateCargo::WriteCargo()
|
||||||
|
|
||||||
cargo.close();
|
cargo.close();
|
||||||
|
|
||||||
|
// Zapisywanie klucza szyfrujšcego
|
||||||
|
//crypt.saveKey(catalogPath);
|
||||||
|
|
||||||
std::cout << "The container was successfully created! " << cargoFile << std::endl;
|
std::cout << "The container was successfully created! " << cargoFile << std::endl;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,8 @@
|
||||||
#define EXTENSION "pak"
|
#define EXTENSION "pak"
|
||||||
#define SIGNATURE "XPAK"
|
#define SIGNATURE "XPAK"
|
||||||
|
|
||||||
|
#define SIGNATURE_KEY_FILE 1497713496 // XKEY
|
||||||
|
|
||||||
#define VERSION 100
|
#define VERSION 100
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,21 @@
|
||||||
#include "EncryptionManager.h"
|
#include "EncryptionManager.h"
|
||||||
|
|
||||||
EncryptionManager::EncryptionManager()
|
EncryptionManager::EncryptionManager()
|
||||||
|
:keyReady(false)
|
||||||
{
|
{
|
||||||
if (sodium_init() < 0) {
|
if (sodium_init() < 0) {
|
||||||
throw std::runtime_error("libsodium init failed");
|
throw std::runtime_error("libsodium init failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
keyReady = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<char> EncryptionManager::encrypt(const std::vector<char>& raw)
|
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());
|
std::vector<char> crypt(raw.size());
|
||||||
|
|
||||||
|
// Generowanie kluczy
|
||||||
|
generateKeys();
|
||||||
|
|
||||||
if (crypto_stream_chacha20_ietf_xor_ic(
|
if (crypto_stream_chacha20_ietf_xor_ic(
|
||||||
reinterpret_cast<unsigned char*>(crypt.data()),
|
reinterpret_cast<unsigned char*>(crypt.data()),
|
||||||
|
|
@ -25,4 +28,67 @@ std::vector<char> EncryptionManager::encrypt(const std::vector<char>& raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
return crypt;
|
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();
|
||||||
}
|
}
|
||||||
|
|
@ -4,6 +4,11 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <fstream>
|
||||||
|
#include <ctime>
|
||||||
|
#include <iostream>
|
||||||
|
#include <boost/crc.hpp>
|
||||||
|
#include "DataStruct.h"
|
||||||
|
|
||||||
class EncryptionManager
|
class EncryptionManager
|
||||||
{
|
{
|
||||||
|
|
@ -14,7 +19,14 @@ public:
|
||||||
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 loadKey(const std::string&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::array<unsigned char, crypto_stream_chacha20_ietf_KEYBYTES> key{};
|
std::array<unsigned char, crypto_stream_chacha20_ietf_KEYBYTES> key{};
|
||||||
std::array<unsigned char, crypto_stream_chacha20_ietf_NONCEBYTES> nonce{};
|
std::array<unsigned char, crypto_stream_chacha20_ietf_NONCEBYTES> nonce{};
|
||||||
|
bool keyReady;
|
||||||
|
|
||||||
|
void generateKeys();
|
||||||
|
uint16_t crc16(const std::vector<char>&);
|
||||||
};
|
};
|
||||||
47
TimeStamp.h
Normal file
47
TimeStamp.h
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
|
class TimeStamp
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TimeStamp()
|
||||||
|
:time(std::time(nullptr))
|
||||||
|
{}
|
||||||
|
~TimeStamp() = default;
|
||||||
|
|
||||||
|
uint32_t get()
|
||||||
|
{
|
||||||
|
#if defined(_WIN32) localtime_s(<, &time);
|
||||||
|
#else localtime_r(<, &time);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
uint16_t d = dosDate(lt);
|
||||||
|
uint16_t t = dosTime(lt);
|
||||||
|
|
||||||
|
uint32_t combined = ((uint32_t)d << 16) | t;
|
||||||
|
return combined;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::time_t time;
|
||||||
|
std::tm lt{};
|
||||||
|
|
||||||
|
uint16_t dosDate(const std::tm& t)
|
||||||
|
{
|
||||||
|
int year = t.tm_year + 1900;
|
||||||
|
int y = (year >= 1980) ? (year - 1980) : 0;
|
||||||
|
int m = t.tm_mon + 1;
|
||||||
|
int d = t.tm_mday;
|
||||||
|
return static_cast<uint16_t>((y << 9) | (m << 5) | d);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t dosTime(const std::tm& t)
|
||||||
|
{
|
||||||
|
int h = t.tm_hour;
|
||||||
|
int min = t.tm_min;
|
||||||
|
int s2 = t.tm_sec / 2;
|
||||||
|
return static_cast<uint16_t>((h << 11) | (min << 5) | s2);
|
||||||
|
}
|
||||||
|
};
|
||||||
19
license/libsodium/LICENSE.txt
Normal file
19
license/libsodium/LICENSE.txt
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
/*
|
||||||
|
* ISC License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013-2025
|
||||||
|
* Frank Denis <j at pureftpd dot org>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
BIN
test.key
Normal file
BIN
test.key
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue