cyptography done
This commit is contained in:
@@ -5,8 +5,9 @@ class Buffer
|
||||
{
|
||||
public:
|
||||
unsigned char* buffer = nullptr;
|
||||
size_t size = 0;
|
||||
Buffer(size_t size)
|
||||
int taken = 0;
|
||||
int size = 0;
|
||||
Buffer(int size)
|
||||
{
|
||||
this->size = size;
|
||||
this->buffer = new unsigned char[size];
|
||||
@@ -22,4 +23,27 @@ public:
|
||||
if (buffer) delete[] buffer;
|
||||
}
|
||||
|
||||
void resize(int new_size)
|
||||
{
|
||||
unsigned char* new_buffer = (unsigned char*)realloc(buffer, new_size);
|
||||
if (!new_buffer)
|
||||
{
|
||||
printf_s("Error resizing buffer\n");
|
||||
return;
|
||||
}
|
||||
|
||||
buffer = new_buffer;
|
||||
size = new_size;
|
||||
}
|
||||
|
||||
void add(void* data, int data_size)
|
||||
{
|
||||
if (taken + data_size > size)
|
||||
{
|
||||
resize(size + data_size);
|
||||
}
|
||||
memcpy_s(buffer + taken, size - taken, data, data_size);
|
||||
taken += data_size;
|
||||
}
|
||||
|
||||
};
|
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include "buffer.h"
|
||||
|
||||
struct evp_cipher_ctx_st;
|
||||
typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX;
|
||||
@@ -8,16 +9,16 @@ typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX;
|
||||
class Cryptography
|
||||
{
|
||||
public:
|
||||
Cryptography(const char* password);
|
||||
Cryptography(const char* password, size_t size);
|
||||
~Cryptography();
|
||||
int encrypt(uint8_t* input_buffer, int32_t input_buffer_size);
|
||||
int decrypt(uint8_t* input_buffer);
|
||||
|
||||
bool encrypt(Buffer* plain, Buffer* encrypted);
|
||||
bool decrypt(Buffer* encrypted, Buffer* decrypted);
|
||||
|
||||
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();
|
||||
bool generate_key_and_iv_from_password(const char* password, size_t size);
|
||||
bool handleErrors();
|
||||
};
|
||||
|
83
Password_manager/include/func.h
Normal file
83
Password_manager/include/func.h
Normal file
@@ -0,0 +1,83 @@
|
||||
#pragma once
|
||||
#include <fstream>
|
||||
#include "buffer.h"
|
||||
|
||||
|
||||
struct Pass
|
||||
{
|
||||
char label[20];
|
||||
char password[20];
|
||||
|
||||
Pass()
|
||||
{
|
||||
memset(label, 0, 20);
|
||||
memset(password, 0, 20);
|
||||
};
|
||||
};
|
||||
|
||||
bool find_password(Buffer* buff, char* label, char* password)
|
||||
{
|
||||
Pass* passwords = (Pass*)buff->buffer;
|
||||
|
||||
for (int i = 0; i < buff->taken / sizeof(Pass); i++)
|
||||
{
|
||||
if (!strcmp(passwords[i].label, label))
|
||||
{
|
||||
strcpy_s(password, 20, passwords[i].password);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void generate_password(char* password)
|
||||
{
|
||||
srand(time(NULL));
|
||||
char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_-+={[}]|:;<,>.?";
|
||||
for (int i = 0; i < 15; i++)
|
||||
{
|
||||
password[i] = characters[rand() % (sizeof(characters) - 1)];
|
||||
}
|
||||
password[15] = '\0';
|
||||
}
|
||||
|
||||
bool save_buffer_to_file(Buffer* buffer)
|
||||
{
|
||||
std::ofstream file("passwords.bin", std::ios::binary);
|
||||
if (!file.is_open())
|
||||
{
|
||||
printf_s("Error saving file\n");
|
||||
return false;
|
||||
}
|
||||
file.write((char*)buffer->buffer, buffer->taken);
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool load_buffer_from_file(Buffer* buffer)
|
||||
{
|
||||
|
||||
std::ifstream file("passwords.bin", std::ios::binary);
|
||||
if (!file.is_open()) return false;
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
buffer->resize(file.tellg());
|
||||
file.seekg(0, std::ios::beg);
|
||||
file.read((char*)buffer->buffer, buffer->size);
|
||||
|
||||
if (file)
|
||||
buffer->taken = buffer->size;
|
||||
else
|
||||
buffer->taken = file.gcount();
|
||||
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void print_usage()
|
||||
{
|
||||
printf_s("Usage:\n");
|
||||
printf_s("password_manager.exe <label>\n");
|
||||
printf_s("password_manager.exe -g <label>\n");
|
||||
}
|
Reference in New Issue
Block a user