Add header file
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
cmake_minimum_required(VERSION 3.24) # Requires 3.24+ for URL-based FetchContent binaries
|
cmake_minimum_required(VERSION 3.24)
|
||||||
project(OpenSSLExample CXX)
|
project(CryptoPDiddy CXX)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
@@ -9,7 +9,6 @@ include(FetchContent)
|
|||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
# Automatically downloads pre-built Windows OpenSSL binaries.
|
# Automatically downloads pre-built Windows OpenSSL binaries.
|
||||||
# The original placeholder URL "https://github.com" cannot be unpacked by FetchContent.
|
|
||||||
FetchContent_Declare(
|
FetchContent_Declare(
|
||||||
openssl
|
openssl
|
||||||
URL "https://github.com/TaurusTLS-Developers/OpenSSL-Distribution/releases/download/v3.5.6/openssl-3.5.6-Windows-x64.zip"
|
URL "https://github.com/TaurusTLS-Developers/OpenSSL-Distribution/releases/download/v3.5.6/openssl-3.5.6-Windows-x64.zip"
|
||||||
@@ -132,22 +131,20 @@ else()
|
|||||||
set(OPENSSL_SSL_LIB OpenSSL::SSL)
|
set(OPENSSL_SSL_LIB OpenSSL::SSL)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_executable(crypto_app main.cpp)
|
add_executable(EpstProject main.cpp CryptEpstein.h)
|
||||||
|
|
||||||
# Link against the downloaded binaries
|
target_include_directories(EpstProject PRIVATE "${OPENSSL_INCLUDE_DIR}")
|
||||||
target_include_directories(crypto_app PRIVATE "${OPENSSL_INCLUDE_DIR}")
|
target_link_libraries(EpstProject PRIVATE "${OPENSSL_CRYPTO_LIB}" "${OPENSSL_SSL_LIB}")
|
||||||
target_link_libraries(crypto_app PRIVATE "${OPENSSL_CRYPTO_LIB}" "${OPENSSL_SSL_LIB}")
|
|
||||||
|
|
||||||
# Copy DLL files to output directory so the executable can run
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
if(NOT OPENSSL_CRYPTO_DLL OR NOT OPENSSL_SSL_DLL)
|
if(NOT OPENSSL_CRYPTO_DLL OR NOT OPENSSL_SSL_DLL)
|
||||||
message(FATAL_ERROR "Downloaded OpenSSL package does not contain the expected DLL files.")
|
message(FATAL_ERROR "Downloaded OpenSSL package does not contain the expected DLL files.")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_custom_command(TARGET crypto_app POST_BUILD
|
add_custom_command(TARGET EpstProject POST_BUILD
|
||||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||||
"${OPENSSL_CRYPTO_DLL}"
|
"${OPENSSL_CRYPTO_DLL}"
|
||||||
"${OPENSSL_SSL_DLL}"
|
"${OPENSSL_SSL_DLL}"
|
||||||
$<TARGET_FILE_DIR:crypto_app>
|
$<TARGET_FILE_DIR:EpstProject>
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
19
CryptEpstein.h
Normal file
19
CryptEpstein.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <cstdio>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <openssl/evp.h>
|
||||||
|
#include <openssl/pem.h>
|
||||||
|
#include <openssl/rand.h>
|
||||||
|
#include <openssl/rsa.h>
|
||||||
|
|
||||||
|
// RAII
|
||||||
|
struct PKEYDeleter { void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); } };
|
||||||
|
struct CTXDeleter { void operator()(EVP_PKEY_CTX* p) const { EVP_PKEY_CTX_free(p); } };
|
||||||
|
struct CipherDeleter { void operator()(EVP_CIPHER_CTX* ctx) const { EVP_CIPHER_CTX_free(ctx); } };
|
||||||
|
struct BIODeleter { void operator()(BIO* b) const { BIO_free_all(b); } };
|
||||||
31
main.cpp
31
main.cpp
@@ -1,31 +1,14 @@
|
|||||||
#include <cstdint>
|
#include "CryptEpstein.h"
|
||||||
#include <cstdio>
|
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
#include <memory>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <openssl/evp.h>
|
using PRIVATE_KEY = std::unique_ptr<EVP_PKEY, PKEYDeleter>;
|
||||||
#include <openssl/pem.h>
|
using PUBLIC_KEY_CONTEXT = std::unique_ptr<EVP_PKEY_CTX, CTXDeleter>;
|
||||||
#include <openssl/rand.h>
|
using KEY_BIO = std::unique_ptr<BIO, BIODeleter>;
|
||||||
#include <openssl/rsa.h>
|
using CIPHER_CONTEXT = std::unique_ptr<EVP_CIPHER_CTX, CipherDeleter>;
|
||||||
|
|
||||||
#define DATA_WRITE(data) reinterpret_cast<const char*>(data)
|
#define DATA_WRITE(data) reinterpret_cast<const char*>(data)
|
||||||
#define DATA_READ(data) reinterpret_cast<char*>(data)
|
#define DATA_READ(data) reinterpret_cast<char*>(data)
|
||||||
|
|
||||||
constexpr size_t BUFFER_SIZE = 4096;
|
constexpr size_t BUFFER_SIZE = 4096;
|
||||||
// RAII
|
|
||||||
struct PKEYDeleter { void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); } };
|
|
||||||
struct CTXDeleter { void operator()(EVP_PKEY_CTX* p) const { EVP_PKEY_CTX_free(p); } };
|
|
||||||
struct CipherDeleter { void operator()(EVP_CIPHER_CTX* ctx) const { EVP_CIPHER_CTX_free(ctx); } };
|
|
||||||
struct BIODeleter { void operator()(BIO* b) const { BIO_free_all(b); } };
|
|
||||||
|
|
||||||
typedef std::unique_ptr<EVP_PKEY, PKEYDeleter> PRIVATE_KEY;
|
|
||||||
typedef std::unique_ptr<EVP_PKEY_CTX, CTXDeleter> PUBLIC_KEY_CONTEXT;
|
|
||||||
typedef std::unique_ptr<BIO, BIODeleter> KEY_BIO;
|
|
||||||
typedef std::unique_ptr<EVP_CIPHER_CTX, CipherDeleter> CIPTHER_CONTEXT;
|
|
||||||
|
|
||||||
void generate_rsa_keypair(const std::string& private_key_path, const std::string& public_key_path) {
|
void generate_rsa_keypair(const std::string& private_key_path, const std::string& public_key_path) {
|
||||||
// Initialize the context for key generation
|
// Initialize the context for key generation
|
||||||
@@ -130,7 +113,7 @@ void hybrid_encrypt(const std::string& input_path, const std::string& output_pat
|
|||||||
out_file.write(DATA_WRITE(iv), sizeof(iv));
|
out_file.write(DATA_WRITE(iv), sizeof(iv));
|
||||||
|
|
||||||
// Stream encrypt the actual file data via AES
|
// Stream encrypt the actual file data via AES
|
||||||
CIPTHER_CONTEXT aes_ctx(EVP_CIPHER_CTX_new());
|
CIPHER_CONTEXT aes_ctx(EVP_CIPHER_CTX_new());
|
||||||
if (!aes_ctx || EVP_EncryptInit_ex(aes_ctx.get(), EVP_aes_256_cbc(), nullptr, aes_key, iv) != 1) {
|
if (!aes_ctx || EVP_EncryptInit_ex(aes_ctx.get(), EVP_aes_256_cbc(), nullptr, aes_key, iv) != 1) {
|
||||||
throw std::runtime_error("AES init failed.");
|
throw std::runtime_error("AES init failed.");
|
||||||
}
|
}
|
||||||
@@ -188,7 +171,7 @@ void hybrid_decrypt(const std::string& input_path, const std::string& output_pat
|
|||||||
aes_key.resize(aes_key_len);
|
aes_key.resize(aes_key_len);
|
||||||
|
|
||||||
// Stream decrypt the file data using the recovered AES key
|
// Stream decrypt the file data using the recovered AES key
|
||||||
CIPTHER_CONTEXT aes_ctx(EVP_CIPHER_CTX_new());
|
CIPHER_CONTEXT aes_ctx(EVP_CIPHER_CTX_new());
|
||||||
if (!aes_ctx || EVP_DecryptInit_ex(aes_ctx.get(), EVP_aes_256_cbc(), nullptr, aes_key.data(), iv) != 1) {
|
if (!aes_ctx || EVP_DecryptInit_ex(aes_ctx.get(), EVP_aes_256_cbc(), nullptr, aes_key.data(), iv) != 1) {
|
||||||
throw std::runtime_error("AES decrypt init failed.");
|
throw std::runtime_error("AES decrypt init failed.");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user