🌿 Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms"

Welcome to the series of C++ projects. Here, I will guide you on how to build useful applications and systems using your C++ skills. These projects won't be extremely easy or beginner-friendly, but I will do my best to explain them in the clearest way possible. Let's build something great!
This project securely manages passwords by storing and retrieving them for different accounts. Passwords are encrypted before being saved to a file, and a master password is required for access.
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
// Simple encryption function
string encryptPassword(const string &password) {
string encrypted = password;
for (char &c : encrypted) {
c += 3; // Shift each character forward by 3
}
return encrypted;
}
// Simple decryption function
string decryptPassword(const string &password) {
string decrypted = password;
for (char &c : decrypted) {
c -= 3; // Shift each character back by 3
}
return decrypted;
}
// Function to load passwords from file
void loadPasswords(map<string, string> &passwordMap) {
ifstream file("passwords.txt");
if (file.is_open()) {
string account, encryptedPassword;
while (file >> account >> encryptedPassword) {
passwordMap[account] = encryptedPassword;
}
file.close();
}
}
// Function to save passwords to file
void savePasswords(const map<string, string> &passwordMap) {
ofstream file("passwords.txt");
if (file.is_open()) {
for (const auto &entry : passwordMap) {
file << entry.first << " " << entry.second << "\n";
}
file.close();
}
}
// Function to add a new password
void addPassword(map<string, string> &passwordMap) {
string account, password;
cout << "Enter the account name: ";
cin >> account;
cout << "Enter the password: ";
cin >> password;
string encryptedPassword = encryptPassword(password);
passwordMap[account] = encryptedPassword;
savePasswords(passwordMap);
cout << "Password saved successfully!\n";
}
// Function to retrieve a password
void retrievePassword(const map<string, string> &passwordMap) {
string account;
cout << "Enter the account name: ";
cin >> account;
auto it = passwordMap.find(account);
if (it != passwordMap.end()) {
string decryptedPassword = decryptPassword(it->second);
cout << "Password for " << account << ": " << decryptedPassword << "\n";
} else {
cout << "Account not found.\n";
}
}
// Main menu function
void menu() {
map<string, string> passwordMap;
loadPasswords(passwordMap);
string masterPassword = "admin123";
string enteredPassword;
cout << "----- Password Manager -----\n";
cout << "Enter the master password to access: ";
cin >> enteredPassword;
if (enteredPassword != masterPassword) {
cout << "Incorrect master password. Access denied.\n";
return;
}
int choice;
do {
cout << "\n---- Menu ----\n";
cout << "1. Add Password\n";
cout << "2. Retrieve Password\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addPassword(passwordMap);
break;
case 2:
retrievePassword(passwordMap);
break;
case 3:
cout << "Exiting the Password Manager. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 3);
}
int main() {
menu();
return 0;
}
Solution: Replace Caesar cipher with a stronger hashing algorithm such as SHA-256.
Solution: Encrypt stored files or use a secure database with strict access control.
Solution: Implement a password recovery mechanism, such as email-based recovery or security questions.
This project is a great way to practice file handling, encryption, and conditional logic in C++. Although the encryption method used here (Caesar cipher) is simple and not suitable for real-world applications, it helps demonstrate the basics of securing passwords. For production-level security, consider using more advanced encryption techniques like AES (Advanced Encryption Standard) or hashing with salt (e.g., SHA-256 + PBKDF2).
I hope you enjoyed reading this blog and gained valuable insights! Stay tuned for more exciting C++ projects in the future!
Comments
Post a Comment