Cryptography WIP
This commit is contained in:
parent
c41127839d
commit
fdcdc1054b
@ -133,11 +133,14 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="source\clipboard.cpp" />
|
<ClCompile Include="source\win.cpp" />
|
||||||
|
<ClCompile Include="source\cryptography.cpp" />
|
||||||
<ClCompile Include="source\main.cpp" />
|
<ClCompile Include="source\main.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="include\clipboard.hpp" />
|
<ClInclude Include="include\buffer.h" />
|
||||||
|
<ClInclude Include="include\win.h" />
|
||||||
|
<ClInclude Include="include\cryptography.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
@ -18,12 +18,21 @@
|
|||||||
<ClCompile Include="source\main.cpp">
|
<ClCompile Include="source\main.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="source\clipboard.cpp">
|
<ClCompile Include="source\win.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="source\cryptography.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="include\clipboard.hpp">
|
<ClInclude Include="include\win.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="include\cryptography.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="include\buffer.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
25
Password_manager/include/Buffer.h
Normal file
25
Password_manager/include/Buffer.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
|
class Buffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
unsigned char* buffer = nullptr;
|
||||||
|
size_t size = 0;
|
||||||
|
Buffer(size_t size)
|
||||||
|
{
|
||||||
|
this->size = size;
|
||||||
|
this->buffer = new unsigned char[size];
|
||||||
|
}
|
||||||
|
|
||||||
|
Buffer() {
|
||||||
|
size = 0;
|
||||||
|
buffer = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
~Buffer()
|
||||||
|
{
|
||||||
|
if (buffer) delete[] buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
@ -1,3 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
bool put_data_on_clipboard(const char* text);
|
|
23
Password_manager/include/cryptography.h
Normal file
23
Password_manager/include/cryptography.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
struct evp_cipher_ctx_st;
|
||||||
|
typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX;
|
||||||
|
|
||||||
|
|
||||||
|
class Cryptography
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Cryptography(const char* password);
|
||||||
|
~Cryptography();
|
||||||
|
int encrypt(uint8_t* input_buffer, int32_t input_buffer_size);
|
||||||
|
int decrypt(uint8_t* input_buffer);
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t key[32] = { 0 };
|
||||||
|
uint8_t iv[16] = { 0 };
|
||||||
|
EVP_CIPHER_CTX* ctx = nullptr;
|
||||||
|
|
||||||
|
bool generate_key_and_iv_from_password(const char* password);
|
||||||
|
void handleErrors();
|
||||||
|
};
|
4
Password_manager/include/win.h
Normal file
4
Password_manager/include/win.h
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
bool put_data_on_clipboard(const char* text);
|
||||||
|
std::string get_passwd();
|
127
Password_manager/source/cryptography.cpp
Normal file
127
Password_manager/source/cryptography.cpp
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
#include <openssl/evp.h>
|
||||||
|
#include <openssl/err.h>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include "cryptography.h"
|
||||||
|
#include "buffer.h"
|
||||||
|
|
||||||
|
Cryptography::Cryptography(const char* password)
|
||||||
|
{
|
||||||
|
OpenSSL_add_all_algorithms();
|
||||||
|
ERR_load_crypto_strings();
|
||||||
|
|
||||||
|
if (!generate_key_and_iv_from_password(password))
|
||||||
|
{
|
||||||
|
std::cout << "Error generating key and IV from password\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Cryptography::~Cryptography()
|
||||||
|
{
|
||||||
|
ERR_free_strings();
|
||||||
|
EVP_cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
int Cryptography::encrypt(uint8_t* input_buffer, int32_t input_buffer_size)
|
||||||
|
{
|
||||||
|
// Allocate memory for the ciphertext
|
||||||
|
int ciphertext_len = 0;
|
||||||
|
|
||||||
|
Buffer encrypted_buffer(input_buffer_size + EVP_CIPHER_block_size(EVP_aes_256_cbc()));
|
||||||
|
|
||||||
|
// Create and initialize the context for encryption
|
||||||
|
ctx = EVP_CIPHER_CTX_new();
|
||||||
|
if (!ctx) handleErrors();
|
||||||
|
|
||||||
|
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) handleErrors();
|
||||||
|
|
||||||
|
// Perform encryption
|
||||||
|
if (1 != EVP_EncryptUpdate(ctx, encrypted_buffer.buffer, &ciphertext_len, input_buffer, input_buffer_size)) handleErrors();
|
||||||
|
|
||||||
|
int final_len;
|
||||||
|
if (1 != EVP_EncryptFinal_ex(ctx, encrypted_buffer.buffer + ciphertext_len, &final_len)) handleErrors();
|
||||||
|
ciphertext_len += final_len;
|
||||||
|
|
||||||
|
// Clean up the context
|
||||||
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
|
|
||||||
|
|
||||||
|
// store the encrypted buffer to file
|
||||||
|
|
||||||
|
std::ofstream file("encrypted_file.bin", std::ios::binary);
|
||||||
|
if (file)
|
||||||
|
{
|
||||||
|
file.write((char*)encrypted_buffer.buffer, ciphertext_len);
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return ciphertext_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Cryptography::decrypt(uint8_t* out_buffer)
|
||||||
|
{
|
||||||
|
// read the encrypted buffer from file
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
std::ifstream file("encrypted_file.bin", std::ios::binary);
|
||||||
|
if (!file) return 0;
|
||||||
|
|
||||||
|
file.seekg(0, std::ios::end);
|
||||||
|
int encrypted_buffer_len = file.tellg();
|
||||||
|
file.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
|
Buffer encrypted_buffer(encrypted_buffer_len);
|
||||||
|
|
||||||
|
file.read((char*)encrypted_buffer.buffer, encrypted_buffer_len);
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ctx = EVP_CIPHER_CTX_new();
|
||||||
|
if (!ctx) handleErrors();
|
||||||
|
|
||||||
|
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) handleErrors();
|
||||||
|
|
||||||
|
int decrypted_len = 0;
|
||||||
|
if (1 != EVP_DecryptUpdate(ctx, out_buffer, &decrypted_len, encrypted_buffer.buffer, encrypted_buffer_len)) handleErrors();
|
||||||
|
int final_len;
|
||||||
|
if (1 != EVP_DecryptFinal_ex(ctx, out_buffer + decrypted_len, &final_len)) handleErrors();
|
||||||
|
decrypted_len += final_len;
|
||||||
|
|
||||||
|
// Clean up the context
|
||||||
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
|
|
||||||
|
return decrypted_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Cryptography::generate_key_and_iv_from_password(const char* password)
|
||||||
|
{
|
||||||
|
int iterations = 10000;
|
||||||
|
|
||||||
|
// Derive key and IV using PBKDF2
|
||||||
|
if (1 != PKCS5_PBKDF2_HMAC(password, strlen(password), nullptr, 0, iterations, EVP_sha256(), 32, key)) return false;
|
||||||
|
if (1 != PKCS5_PBKDF2_HMAC(password, strlen(password), nullptr, 0, iterations, EVP_sha256(), 16, iv)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cryptography::handleErrors()
|
||||||
|
{
|
||||||
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
|
|
||||||
|
while (auto error = ERR_get_error())
|
||||||
|
{
|
||||||
|
char* error_string = ERR_error_string(error, nullptr);
|
||||||
|
std::cout << error_string << "\n";
|
||||||
|
}
|
||||||
|
abort();
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include "win.h"
|
||||||
#include "clipboard.hpp"
|
|
||||||
|
|
||||||
struct Pass
|
struct Pass
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "clipboard.hpp"
|
#include <string>
|
||||||
|
#include "win.h"
|
||||||
|
|
||||||
bool put_data_on_clipboard(const char* text) {
|
bool put_data_on_clipboard(const char* text) {
|
||||||
int len = strlen(text);
|
int len = strlen(text);
|
||||||
@ -35,4 +36,33 @@ bool put_data_on_clipboard(const char* text) {
|
|||||||
// Clean up and close the clipboard
|
// Clean up and close the clipboard
|
||||||
CloseClipboard();
|
CloseClipboard();
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string get_passwd()
|
||||||
|
{
|
||||||
|
|
||||||
|
HANDLE hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
||||||
|
DWORD mode = 0;
|
||||||
|
|
||||||
|
// Create a restore point Mode
|
||||||
|
// is know 503
|
||||||
|
GetConsoleMode(hStdInput, &mode);
|
||||||
|
|
||||||
|
// Enable echo input
|
||||||
|
// set to 499
|
||||||
|
SetConsoleMode( hStdInput, mode & (~ENABLE_ECHO_INPUT));
|
||||||
|
|
||||||
|
// Take input
|
||||||
|
std::string ipt;
|
||||||
|
std::getline(std::cin, ipt);
|
||||||
|
|
||||||
|
// Otherwise next cout will print
|
||||||
|
// into the same line
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
// Restore the mode
|
||||||
|
SetConsoleMode(hStdInput, mode);
|
||||||
|
|
||||||
|
return ipt;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user