From 72b68b97b61ddf0146c2f92f5906b40571e68098 Mon Sep 17 00:00:00 2001 From: nikola Date: Thu, 20 Jun 2024 12:54:56 +0200 Subject: [PATCH] remove cryptography class and use aes256 class --- include/arg_func.hpp | 13 ++++++------- include/cryptography.hpp | 21 --------------------- main.cpp | 21 +++++++++------------ source/arg_func.cpp | 27 +++++++++++++-------------- source/cryptography.cpp | 36 ------------------------------------ 5 files changed, 28 insertions(+), 90 deletions(-) delete mode 100644 include/cryptography.hpp delete mode 100644 source/cryptography.cpp diff --git a/include/arg_func.hpp b/include/arg_func.hpp index 7283c18..2891aa0 100644 --- a/include/arg_func.hpp +++ b/include/arg_func.hpp @@ -3,7 +3,6 @@ struct LoginInfoPointer; class Buffer; -class Cryptography; #include #include @@ -28,23 +27,23 @@ Arg get_args(int argc, char **argv, char **label); std::optional arg_get(Buffer &decrypted_buffer, const char *label); -std::optional arg_new_password(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto, bool generate); +std::optional arg_new_password(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, std::string &key, bool generate); -void arg_username(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto); +void arg_username(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, std::string &key); -void arg_label_name(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto); +void arg_label_name(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, std::string &key); void arg_list(Buffer &decrypted_buffer); -void arg_delete(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto); +void arg_delete(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, std::string &key); void arg_print_all_p(Buffer &decrypted_buffer, std::string &user_pass); -void arg_change(Buffer &decrypted_buffer, Buffer &encrypted_buffer, std::string &user_pass, Cryptography &crypto); +void arg_change(Buffer &decrypted_buffer, Buffer &encrypted_buffer, std::string &user_pass); void arg_show(Buffer &decrypted_buffer, const char *label); -void arg_file(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto, std::string &save_location_path); +void arg_file(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, std::string &key, std::string &save_location_path); void arg_size(Buffer &encrypted_buffer); diff --git a/include/cryptography.hpp b/include/cryptography.hpp deleted file mode 100644 index 730e092..0000000 --- a/include/cryptography.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef CRYPTOGRAPHY_HPP -#define CRYPTOGRAPHY_HPP - -#include - -class Buffer; - -class Cryptography -{ -public: - Cryptography(std::string password); - ~Cryptography(); - void change_pass(std::string password); - bool encrypt(Buffer &plain, Buffer &encrypted); - bool decrypt(Buffer &encrypted, Buffer &decrypted); - -private: - std::string key; -}; - -#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 7ff79d9..0077eed 100644 --- a/main.cpp +++ b/main.cpp @@ -4,7 +4,7 @@ #include "sys.hpp" #include "buffer.hpp" -#include "cryptography.hpp" +#include "aes256.hpp" #include "func.hpp" #include "arg_func.hpp" @@ -74,13 +74,10 @@ int main(int argc, char **argv) } #endif // !1 - Cryptography crypto(user_pass); - Buffer decrypted_buffer; // check if encrypted buffer is empty if not, decrypt it if (encrypted_buffer.taken > 0) - if (!crypto.decrypt(encrypted_buffer, decrypted_buffer)) - return 1; + Aes256::decrypt(user_pass, encrypted_buffer, decrypted_buffer); // if decrypted buffer is empty, add index if (decrypted_buffer.taken < sizeof(Index)) @@ -107,7 +104,7 @@ int main(int argc, char **argv) break; case Arg::Generate: - login_info = arg_new_password(decrypted_buffer, encrypted_buffer, label, crypto, true); + login_info = arg_new_password(decrypted_buffer, encrypted_buffer, label, user_pass, true); break; case Arg::List: @@ -115,7 +112,7 @@ int main(int argc, char **argv) break; case Arg::Delete: - arg_delete(decrypted_buffer, encrypted_buffer, label, crypto); + arg_delete(decrypted_buffer, encrypted_buffer, label, user_pass); break; case Arg::Print_all_p: @@ -123,19 +120,19 @@ int main(int argc, char **argv) break; case Arg::Input: - login_info = arg_new_password(decrypted_buffer, encrypted_buffer, label, crypto, false); + login_info = arg_new_password(decrypted_buffer, encrypted_buffer, label, user_pass, false); break; case Arg::Username: - arg_username(decrypted_buffer, encrypted_buffer, label, crypto); + arg_username(decrypted_buffer, encrypted_buffer, label, user_pass); break; case Arg::Name: - arg_label_name(decrypted_buffer, encrypted_buffer, label, crypto); + arg_label_name(decrypted_buffer, encrypted_buffer, label, user_pass); break; case Arg::Change: - arg_change(decrypted_buffer, encrypted_buffer, user_pass, crypto); + arg_change(decrypted_buffer, encrypted_buffer, user_pass); break; case Arg::Show: @@ -143,7 +140,7 @@ int main(int argc, char **argv) break; case Arg::File: - arg_file(decrypted_buffer, encrypted_buffer, label, crypto, save_location_path.value()); + arg_file(decrypted_buffer, encrypted_buffer, label, user_pass, save_location_path.value()); break; case Arg::Size: diff --git a/source/arg_func.cpp b/source/arg_func.cpp index 555d0cc..e7a2425 100644 --- a/source/arg_func.cpp +++ b/source/arg_func.cpp @@ -12,7 +12,7 @@ #include "sys.hpp" #include "func.hpp" #include "buffer.hpp" -#include "cryptography.hpp" +#include "aes256.hpp" void print_args() { @@ -104,7 +104,7 @@ std::optional arg_get(Buffer &decrypted_buffer, const char *la return get_logininfo_pointer_from_buffer(decrypted_buffer, pass); } -std::optional arg_new_password(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto, bool generate) +std::optional arg_new_password(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, std::string &key, bool generate) { int pass = find_logininfo_in_buffer(decrypted_buffer, label); std::string username = ""; @@ -156,13 +156,13 @@ std::optional arg_new_password(Buffer &decrypted_buffer, Buffe } add_logininfo_to_buffer(decrypted_buffer, name.c_str(), username.c_str(), password.c_str()); - crypto.encrypt(decrypted_buffer, encrypted_buffer); + Aes256::encrypt(key, decrypted_buffer, encrypted_buffer); encrypted_buffer.save_to_file(); Index *index = (Index *)decrypted_buffer.buffer; return get_logininfo_pointer_from_buffer(decrypted_buffer, index->count - 1); } -void arg_username(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto) +void arg_username(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, std::string &key) { int pass = find_logininfo_in_buffer(decrypted_buffer, label); if (pass < 0) @@ -181,11 +181,11 @@ void arg_username(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char delete_logininfo_from_buffer(decrypted_buffer, pass); add_logininfo_to_buffer(decrypted_buffer, name.c_str(), username.c_str(), password.c_str()); - crypto.encrypt(decrypted_buffer, encrypted_buffer); + Aes256::encrypt(key, decrypted_buffer, encrypted_buffer); encrypted_buffer.save_to_file(); } -void arg_label_name(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto) +void arg_label_name(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, std::string &key) { int pass = find_logininfo_in_buffer(decrypted_buffer, label); @@ -205,7 +205,7 @@ void arg_label_name(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const ch delete_logininfo_from_buffer(decrypted_buffer, pass); add_logininfo_to_buffer(decrypted_buffer, name.c_str(), username.c_str(), password.c_str()); - crypto.encrypt(decrypted_buffer, encrypted_buffer); + Aes256::encrypt(key, decrypted_buffer, encrypted_buffer); encrypted_buffer.save_to_file(); } @@ -229,7 +229,7 @@ void arg_list(Buffer &decrypted_buffer) } } -void arg_delete(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label_to_del, Cryptography &crypto) +void arg_delete(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label_to_del, std::string &key) { printf("Deleting password for %s\n", label_to_del); int pass = find_logininfo_in_buffer(decrypted_buffer, label_to_del); @@ -241,7 +241,7 @@ void arg_delete(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char * delete_logininfo_from_buffer(decrypted_buffer, pass); - crypto.encrypt(decrypted_buffer, encrypted_buffer); + Aes256::encrypt(key, decrypted_buffer, encrypted_buffer); encrypted_buffer.save_to_file(); printf("Password deleted\n"); } @@ -273,7 +273,7 @@ void arg_print_all_p(Buffer &decrypted_buffer, std::string &user_pass) } } -void arg_change(Buffer &decrypted_buffer, Buffer &encrypted_buffer, std::string &user_pass, Cryptography &crypto) +void arg_change(Buffer &decrypted_buffer, Buffer &encrypted_buffer, std::string &user_pass) { printf("Input main password for confirmation:"); @@ -312,8 +312,7 @@ void arg_change(Buffer &decrypted_buffer, Buffer &encrypted_buffer, std::string return; } - crypto.change_pass(new_string); - crypto.encrypt(decrypted_buffer, encrypted_buffer); + Aes256::encrypt(new_string, decrypted_buffer, encrypted_buffer); encrypted_buffer.save_to_file(); printf("Password changed\n"); } @@ -333,7 +332,7 @@ void arg_show(Buffer &decrypted_buffer, const char *label) } } -void arg_file(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto, std::string &save_location_path) +void arg_file(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, std::string &key, std::string &save_location_path) { namespace fs = std::filesystem; @@ -348,7 +347,7 @@ void arg_file(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *la if (decrypted_buffer.taken <= sizeof(Index)) return; - crypto.encrypt(decrypted_buffer, encrypted_buffer); + Aes256::encrypt(key, decrypted_buffer, encrypted_buffer); encrypted_buffer.save_to_file(save.string()); } diff --git a/source/cryptography.cpp b/source/cryptography.cpp deleted file mode 100644 index 75c72a7..0000000 --- a/source/cryptography.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include -#include - -#include "cryptography.hpp" -#include "buffer.hpp" -#include "aes256.hpp" - -Cryptography::Cryptography(std::string password) -{ - key = password; -} - -Cryptography::~Cryptography() -{ -} - -void Cryptography::change_pass(std::string password) -{ - key = password; -} - -bool Cryptography::encrypt(Buffer &plain, Buffer &encrypted) -{ - - Aes256::encrypt(key, plain, encrypted); - - return true; -} - -bool Cryptography::decrypt(Buffer &encrypted, Buffer &decrypted) -{ - Aes256::decrypt(key, encrypted, decrypted); - return true; -} \ No newline at end of file