forked from parakramgambhir14/CryptVault
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvault.cpp
More file actions
145 lines (115 loc) · 3.75 KB
/
vault.cpp
File metadata and controls
145 lines (115 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "vault.h"
#include "../security/security.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;
// ================= START VAULT (MENU) =================
void startVault(string filename) {
// Load entries from file (encrypted passwords)
vector<Entry> vault = loadEntries(filename);
int choice;
do {
cout << "\n====== 🔐 CRYPTVAULT ======\n";
cout << "1. Add Entry\n";
cout << "2. View Vault\n";
cout << "3. Search Entry\n";
cout << "4. Delete Entry\n";
cout << "5. Exit\n";
cout << "===========================\n";
cout << "Choice: ";
cin >> choice;
cin.ignore(); // clear newline
switch(choice) {
case 1: addEntry(vault); break;
case 2: viewEntries(vault); break;
case 3: searchEntry(vault); break;
case 4: deleteEntry(vault); break;
}
// Always save vault after any operation
saveEntries(filename, vault);
} while(choice != 5);
}
// ================= LOAD ENTRIES =================
vector<Entry> loadEntries(string filename) {
vector<Entry> vault;
ifstream file(filename);
string line;
while(getline(file, line)) {
stringstream ss(line);
Entry e;
getline(ss, e.site, '|');
getline(ss, e.username, '|');
getline(ss, e.password, '|');
// Decrypt password before storing in memory
e.password = decrypt(e.password);
vault.push_back(e);
}
return vault;
}
// ================= SAVE ENTRIES =================
void saveEntries(string filename, vector<Entry>& vault) {
ofstream file(filename);
for(auto &e : vault) {
// Encrypt password before writing to file
file << e.site << "|" << e.username << "|" << encrypt(e.password) << endl;
}
}
// ================= ADD ENTRY =================
void addEntry(vector<Entry>& vault) {
Entry e;
cout << "Site: "; getline(cin, e.site);
cout << "Username: "; getline(cin, e.username);
cout << "Password: "; getline(cin, e.password);
// Password strength check
int score = passwordStrength(e.password);
if(score < 3) {
cout << "Warning: Password is weak!\n";
}
vault.push_back(e);
cout << "Entry added successfully.\n";
}
// ================= VIEW ENTRIES =================
void viewEntries(vector<Entry>& vault) {
if(vault.empty()) {
cout << "Vault is empty.\n";
return;
}
for(size_t i=0; i<vault.size(); i++) {
cout << i+1 << ". "
<< vault[i].site << " | "
<< vault[i].username << " | "
<< vault[i].password << "\n";
}
}
// ================= SEARCH ENTRY =================
void searchEntry(vector<Entry>& vault) {
string site;
cout << "Enter site to search: ";
getline(cin, site);
bool found = false;
for(auto &e : vault) {
if(e.site == site) {
cout << "Found: " << e.site << " | " << e.username << " | " << e.password << "\n";
found = true;
break;
}
}
if(!found) cout << "No entry found for site: " << site << "\n";
}
// ================= DELETE ENTRY =================
void deleteEntry(vector<Entry>& vault) {
string site;
cout << "Enter site to delete: ";
getline(cin, site);
auto it = remove_if(vault.begin(), vault.end(), [&](Entry &e){
return e.site == site;
});
if(it != vault.end()) {
vault.erase(it, vault.end());
cout << "Entry deleted.\n";
} else {
cout << "No entry found to delete.\n";
}
}