Skip to content

Commit

Permalink
24-04-24 Password Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-stepien-dev committed Apr 24, 2024
1 parent 4f35c61 commit 6835555
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions 24-04-2024 Password Manager/PasswordManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <fstream>
#include <iostream>
#include <string>

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";
}
}
};
39 changes: 39 additions & 0 deletions 24-04-2024 Password Manager/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <fstream>
#include <iostream>
#include <string>

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";
}
}
};

0 comments on commit 6835555

Please sign in to comment.