-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuth_System.cpp
More file actions
242 lines (205 loc) Β· 7.05 KB
/
Copy pathAuth_System.cpp
File metadata and controls
242 lines (205 loc) Β· 7.05 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
InternGrow C++ Programming Track
TASK 2: Secure User Authentication System (WITH UPGRADE FEATURE)
--------------------------------------------------------------------
Basic Feature:
- User Registration (Sign Up) with duplicate username check
- User Login (validation against stored credentials)
- Credentials securely stored in an external file (users.txt)
Upgrade Feature:
- Lockout Mechanism: 3 consecutive wrong password attempts
se account temporarily lock ho jata hai for a fixed duration.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <ctime>
using namespace std;
const char* USER_FILE = "users.txt";
const char* LOCKOUT_FILE = "lockout.txt";
const int MAX_ATTEMPTS = 3;
const int LOCKOUT_DURATION = 60; // seconds (demo ke liye 60 sec, aap 300/600 bhi rakh sakte hain)
// ---------- Struct: Lockout info track karne ke liye ----------
struct LockoutInfo {
int failedAttempts;
time_t lockoutUntil; // 0 matlab lock nahi hai
};
// ---------- Simple obfuscation (basic security) ----------
// Password ko plain text ke bajaye thora shift kar ke store karta hai
string encodePassword(string pass) {
for (size_t i = 0; i < pass.size(); i++) {
pass[i] = pass[i] + 3; // Caesar-cipher jaisa simple shift
}
return pass;
}
string decodePassword(string pass) {
for (size_t i = 0; i < pass.size(); i++) {
pass[i] = pass[i] - 3;
}
return pass;
}
// ---------- Check karna ke username pehle se maujood hai ----------
bool usernameExists(const string &username) {
ifstream file(USER_FILE);
string line;
while (getline(file, line)) {
stringstream ss(line);
string storedUser;
getline(ss, storedUser, ',');
if (storedUser == username) {
file.close();
return true;
}
}
file.close();
return false;
}
// ---------- Naya user register karna ----------
void registerUser() {
string username, password;
cout << "\n===== Register New User =====\n";
cout << "Username enter karo: ";
cin >> username;
if (username.empty()) {
cout << "Username khali nahi ho sakta!\n";
return;
}
if (usernameExists(username)) {
cout << "Ye username pehle se maujood hai! Doosra choose karo.\n";
return;
}
cout << "Password enter karo (min 6 characters): ";
cin >> password;
if (password.length() < 6) {
cout << "Password kam se kam 6 characters ka hona chahiye!\n";
return;
}
// File mein append mode se save karna: username,encodedPassword
ofstream file(USER_FILE, ios::app);
file << username << "," << encodePassword(password) << "\n";
file.close();
cout << "β
Registration Successful! Ab aap login kar sakte hain.\n";
}
// ---------- Lockout data ko file se load karna ----------
map<string, LockoutInfo> loadLockoutData() {
map<string, LockoutInfo> data;
ifstream file(LOCKOUT_FILE);
string username;
int attempts;
long lockUntil;
while (file >> username >> attempts >> lockUntil) {
data[username] = {attempts, (time_t)lockUntil};
}
file.close();
return data;
}
// ---------- Lockout data ko file mein save karna ----------
void saveLockoutData(map<string, LockoutInfo> &data) {
ofstream file(LOCKOUT_FILE);
for (map<string, LockoutInfo>::iterator it = data.begin(); it != data.end(); ++it) {
file << it->first << " " << it->second.failedAttempts
<< " " << (long)it->second.lockoutUntil << "\n";
}
file.close();
}
// ---------- User login karna (lockout logic ke saath) ----------
void loginUser() {
string username, password;
cout << "\n===== User Login =====\n";
cout << "Username enter karo: ";
cin >> username;
map<string, LockoutInfo> lockoutData = loadLockoutData();
time_t now = time(0);
// ----- STEP 1: Check karo account lock to nahi hai -----
if (lockoutData.find(username) != lockoutData.end()) {
LockoutInfo &info = lockoutData[username];
if (info.lockoutUntil > now) {
int remaining = (int)(info.lockoutUntil - now);
cout << "π Account lock hai! " << remaining
<< " seconds baad dobara try karein.\n";
return;
}
else if (info.lockoutUntil != 0 && info.lockoutUntil <= now) {
// Lockout time khatam ho chuka, attempts reset karo
info.failedAttempts = 0;
info.lockoutUntil = 0;
}
}
// ----- STEP 2: Username file mein exist karta hai ya nahi -----
if (!usernameExists(username)) {
cout << "β Ye username registered nahi hai.\n";
return;
}
cout << "Password enter karo: ";
cin >> password;
// ----- STEP 3: Credentials verify karna -----
ifstream file(USER_FILE);
string line;
bool matched = false;
while (getline(file, line)) {
stringstream ss(line);
string storedUser, storedPass;
getline(ss, storedUser, ',');
getline(ss, storedPass, ',');
if (storedUser == username && decodePassword(storedPass) == password) {
matched = true;
break;
}
}
file.close();
if (matched) {
cout << "β
Login Successful! Welcome, " << username << ".\n";
// Successful login par attempts reset ho jate hain
lockoutData[username] = {0, 0};
saveLockoutData(lockoutData);
}
else {
// ----- STEP 4: Galat password -> failed attempt count badhana -----
LockoutInfo &info = lockoutData[username];
info.failedAttempts++;
cout << "β Galat password! Attempt " << info.failedAttempts
<< " of " << MAX_ATTEMPTS << ".\n";
if (info.failedAttempts >= MAX_ATTEMPTS) {
info.lockoutUntil = now + LOCKOUT_DURATION;
cout << "π 3 consecutive galat attempts ho gaye! Account "
<< LOCKOUT_DURATION << " seconds ke liye lock kar diya gaya hai.\n";
}
saveLockoutData(lockoutData);
}
}
// ---------- MAIN MENU ----------
int main() {
int choice;
do {
cout << "\n========== Secure User Authentication System ==========\n";
cout << "1. Register\n";
cout << "2. Login\n";
cout << "3. Exit\n";
cout << "Apna choice enter karo: ";
cin >> choice;
// Agar invalid (non-numeric) input aaye to cin ko reset karna
// taake infinite loop na bane
if (cin.fail()) {
cin.clear();
cin.ignore(10000, '\n');
cout << "Invalid input! Sirf number (1-3) enter karein.\n";
continue;
}
switch (choice) {
case 1:
registerUser();
break;
case 2:
loginUser();
break;
case 3:
cout << "Program band ho raha hai. Shukriya!\n";
break;
default:
cout << "Invalid choice! Dobara try karein.\n";
}
} while (choice != 3);
return 0;
}