From 6835555169d956e956af983282ba4b7351654b33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20St=C4=99pie=C5=84?= Date: Wed, 24 Apr 2024 08:21:02 +0200 Subject: [PATCH] 24-04-24 Password Manager --- .idea/.gitignore | 2 + .../PasswordManager.cpp | 66 +++++++++++++++++++ 24-04-2024 Password Manager/main.cpp | 39 +++++++++++ 3 files changed, 107 insertions(+) create mode 100644 24-04-2024 Password Manager/PasswordManager.cpp create mode 100644 24-04-2024 Password Manager/main.cpp diff --git a/.idea/.gitignore b/.idea/.gitignore index 13566b8..a9d7db9 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -6,3 +6,5 @@ # Datasource local storage ignored files /dataSources/ /dataSources.local.xml +# GitHub Copilot persisted chat sessions +/copilot/chatSessions diff --git a/24-04-2024 Password Manager/PasswordManager.cpp b/24-04-2024 Password Manager/PasswordManager.cpp new file mode 100644 index 0000000..e4a9406 --- /dev/null +++ b/24-04-2024 Password Manager/PasswordManager.cpp @@ -0,0 +1,66 @@ +#include +#include +#include + +class PasswordManager { +private: + std::string username; + std::string password; + int shift = 3; // Shift value for Caesar cipher + + std::string encrypt(const std::string& text) { + std::string encrypted_text; + for (char c : text) { + if (isalpha(c)) { + char base = isupper(c) ? 'A' : 'a'; + c = ((c - base + shift) % 26) + base; + } + encrypted_text += c; + } + return encrypted_text; + } + + std::string decrypt(const std::string& text) { + std::string decrypted_text; + for (char c : text) { + if (isalpha(c)) { + char base = isupper(c) ? 'A' : 'a'; + c = ((c - base - shift + 26) % 26) + base; + } + decrypted_text += c; + } + return decrypted_text; + } + +public: + void setData(std::string username, std::string password) { + this->username = username; + this->password = password; + } + + void saveToFile(std::string fileName) { + std::ofstream file; + file.open(fileName); + if (file.is_open()) { + file << encrypt(username) << "\n" << encrypt(password); + file.close(); + } else { + std::cout << "Unable to open file for writing.\n"; + } + } + + void readFromFile(std::string fileName) { + std::ifstream file; + file.open(fileName); + if (file.is_open()) { + std::getline(file, username); + std::getline(file, password); + file.close(); + username = decrypt(username); + password = decrypt(password); + std::cout << "Username: " << username << "\nPassword: " << password << "\n"; + } else { + std::cout << "Unable to open file for reading.\n"; + } + } +}; \ No newline at end of file diff --git a/24-04-2024 Password Manager/main.cpp b/24-04-2024 Password Manager/main.cpp new file mode 100644 index 0000000..b6334cd --- /dev/null +++ b/24-04-2024 Password Manager/main.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +class PasswordManager { +private: + std::string username; + std::string password; + +public: + void setData(std::string username, std::string password) { + this->username = username; + this->password = password; + } + + void saveToFile(std::string fileName) { + std::ofstream file; + file.open(fileName); + if (file.is_open()) { + file << username << "\n" << password; + file.close(); + } else { + std::cout << "Unable to open file for writing.\n"; + } + } + + void readFromFile(std::string fileName) { + std::ifstream file; + file.open(fileName); + if (file.is_open()) { + std::getline(file, username); + std::getline(file, password); + file.close(); + std::cout << "Username: " << username << "\nPassword: " << password << "\n"; + } else { + std::cout << "Unable to open file for reading.\n"; + } + } +}; \ No newline at end of file