Repository navigation
Expand file tree
/
Copy pathpassword_manager.cpp
More file actions
204 lines (183 loc) · 5.83 KB
/
Copy pathpassword_manager.cpp
File metadata and controls
204 lines (183 loc) · 5.83 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <vector>
#include <string>
#include <regex>
#include <algorithm> // For std::remove_if
// Class to manage user passwords (create, validate, encrypt, and verify)
class UserPasswordManager
{
public:
bool validPass; // Flag to indicate if the password is valid
std::string currentPass; // Stores the current password
std::vector<std::string> oldPass; // Stores old passwords
int upperOffset; // Uppercase letter offset
int lowerOffset; // Lowercase letter offset
int digitOffset; // Digit offset
std::string encryptedPassword; // Stores the encrypted password
// Function to create a new password
void createPassword()
{
std::cout << "\nPlease enter your password: ";
getline(std::cin, currentPass);
validPass = validatePassword();
}
// Function to validate the password based on specific criteria (length, characters, etc.)
bool validatePassword()
{
std::regex passwordPattern{"(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9]).{8,}"};
while (!std::regex_match(currentPass, passwordPattern))
{
std::cout << "\nInvalid password\n";
std::cout << "\nPlease enter your password: ";
getline(std::cin, currentPass);
}
for (auto &pass : oldPass)
{
if (currentPass == pass)
{
std::cout << "\nPassword has already been used.";
return false;
}
}
return true;
}
// Function to calculate the offsets for uppercase, lowercase, and digits
void calculateOffsets()
{
upperOffset = 0;
lowerOffset = 0;
digitOffset = 0;
for (auto c : currentPass)
{
if (std::isupper(c))
{
upperOffset += (c - 'A' + 1) % 26;
}
else if (std::islower(c))
{
lowerOffset += (c - 'a' + 1) % 26;
}
else if (std::isdigit(c))
{
digitOffset += (c - '0') % 10;
}
}
}
// Function to encrypt the password using calculated offsets
void encryptPassword()
{
int upperSum = 0, lowerSum = 0, digitSum = 0;
encryptedPassword = " "; // Initialize with a blank space
for (auto c : currentPass)
{
if (std::isalpha(c))
{
if (std::isupper(c))
{
upperSum += int(c);
}
else if (std::islower(c))
{
lowerSum += int(c);
}
}
else if (std::isdigit(c))
{
digitSum += int(c);
}
}
int upperOffset = (upperSum % 26) + 1;
int lowerOffset = (lowerSum % 26) + 1;
int digitOffset = digitSum % 10;
encryptedPassword += std::to_string(upperOffset);
encryptedPassword += std::to_string(lowerOffset);
encryptedPassword += std::to_string(digitOffset);
// Encrypt characters based on their types
for (auto c : currentPass)
{
if (std::isalpha(c))
{
if (std::isupper(c))
{
char encryptedChar = ((c - 'A' + upperOffset) % 26) + 'A';
encryptedPassword += encryptedChar;
}
else if (std::islower(c))
{
char encryptedChar = ((c - 'a' + lowerOffset) % 26) + 'a';
encryptedPassword += encryptedChar;
}
}
else if (std::isdigit(c))
{
char encryptedChar = ((c - '0' + digitOffset) % 10) + '0';
encryptedPassword += encryptedChar;
}
else
{
encryptedPassword += c; // For special characters
}
}
}
// Constructor to initialize the object and create the password
UserPasswordManager()
{
createPassword();
if (validPass)
{
std::cout << "\nPassword Created";
encryptPassword();
std::cout << "\nEncrypted Password: " << encryptedPassword;
}
else
{
std::cout << "\nPassword could not be created";
}
}
// Function to change the password
void changePassword()
{
std::cout << "\nEnter the encrypted password below!";
createPassword();
if (validPass)
{
oldPass.push_back(encryptedPassword); // Add the encrypted password to oldPass vector
std::cout << "\nPassword was successfully changed";
}
else
{
std::cout << "\nPassword was not changed";
}
}
// Function to verify the password
bool verifyPassword(std::string passwordToVerify)
{
std::string encryptedToVerify = passwordToVerify;
encryptedPassword.erase(std::remove_if(encryptedPassword.begin(), encryptedPassword.end(), ::isspace), encryptedPassword.end());
if (encryptedPassword == encryptedToVerify)
{
return true;
}
else
{
return false;
}
}
};
int main()
{
UserPasswordManager userManager; // Create a UserPasswordManager object
userManager.changePassword(); // Change the password
std::string passwordToVerify;
std::cout << "\nEnter the password to verify: ";
getline(std::cin, passwordToVerify);
if (userManager.verifyPassword(passwordToVerify))
{
std::cout << "\nPassword Verified!";
}
else
{
std::cout << "\nPassword Verification Failed!";
}
return 0;
}