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
|
||||
project(OpenSSLExample CXX)
|
||||
cmake_minimum_required(VERSION 3.24)
|
||||
project(CryptoPDiddy CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
@@ -9,7 +9,6 @@ include(FetchContent)
|
||||
|
||||
if(WIN32)
|
||||
# Automatically downloads pre-built Windows OpenSSL binaries.
|
||||
# The original placeholder URL "https://github.com" cannot be unpacked by FetchContent.
|
||||
FetchContent_Declare(
|
||||
openssl
|
||||
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)
|
||||
endif()
|
||||
|
||||
add_executable(crypto_app main.cpp)
|
||||
add_executable(EpstProject main.cpp CryptEpstein.h)
|
||||
|
||||
# Link against the downloaded binaries
|
||||
target_include_directories(crypto_app PRIVATE "${OPENSSL_INCLUDE_DIR}")
|
||||
target_link_libraries(crypto_app PRIVATE "${OPENSSL_CRYPTO_LIB}" "${OPENSSL_SSL_LIB}")
|
||||
target_include_directories(EpstProject PRIVATE "${OPENSSL_INCLUDE_DIR}")
|
||||
target_link_libraries(EpstProject PRIVATE "${OPENSSL_CRYPTO_LIB}" "${OPENSSL_SSL_LIB}")
|
||||
|
||||
# Copy DLL files to output directory so the executable can run
|
||||
if(WIN32)
|
||||
if(NOT OPENSSL_CRYPTO_DLL OR NOT OPENSSL_SSL_DLL)
|
||||
message(FATAL_ERROR "Downloaded OpenSSL package does not contain the expected DLL files.")
|
||||
endif()
|
||||
|
||||
add_custom_command(TARGET crypto_app POST_BUILD
|
||||
add_custom_command(TARGET EpstProject POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
"${OPENSSL_CRYPTO_DLL}"
|
||||
"${OPENSSL_SSL_DLL}"
|
||||
$<TARGET_FILE_DIR:crypto_app>
|
||||
$<TARGET_FILE_DIR:EpstProject>
|
||||
)
|
||||
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 <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "CryptEpstein.h"
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/rsa.h>
|
||||
using PRIVATE_KEY = std::unique_ptr<EVP_PKEY, PKEYDeleter>;
|
||||
using PUBLIC_KEY_CONTEXT = std::unique_ptr<EVP_PKEY_CTX, CTXDeleter>;
|
||||
using KEY_BIO = std::unique_ptr<BIO, BIODeleter>;
|
||||
using CIPHER_CONTEXT = std::unique_ptr<EVP_CIPHER_CTX, CipherDeleter>;
|
||||
|
||||
#define DATA_WRITE(data) reinterpret_cast<const char*>(data)
|
||||
#define DATA_READ(data) reinterpret_cast<char*>(data)
|
||||
|
||||
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) {
|
||||
// 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));
|
||||
|
||||
// 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) {
|
||||
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);
|
||||
|
||||
// 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) {
|
||||
throw std::runtime_error("AES decrypt init failed.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user