-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
174 lines (133 loc) · 3.56 KB
/
Copy pathmain.cpp
File metadata and controls
174 lines (133 loc) · 3.56 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
//
// main.cpp
// Secure PIN Authentication sysytem
//
// Created by AKE<3
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
const string FILE_NAME = "credentials.txt";
bool isValidPIN(const string& pin);
string encryptPIN(const string& pin);
string decryptPIN(const string& encryptedPIN);
bool pinFileExists();
void setupPIN();
bool login();
int main() {
int choice;
cout << " Secure PIN Authentication\n";
cout << "---------------------------\n";
do {
cout << "\nMenu:\n";
cout << "1. Set up PIN\n";
cout << "2. Login\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
setupPIN();
}
else if (choice == 2) {
bool success = login();
if (success) {
cout << "\nAccess granted. Welcome!\n";
} else {
cout << "\nAccess denied.\n";
}
}
else if (choice == 3) {
cout << "\nExiting program...\n";
}
else {
cout << "\nInvalid choice. Please try again.\n";
}
} while (choice != 3);
return 0;
}
bool isValidPIN(const string& pin) {
if (pin.length() != 4) {
return false;
}
for (char ch : pin) {
if (!isdigit(ch)) {
return false;
}
}
return true;
}
string encryptPIN(const string& pin) {
string encryptedPIN = "";
for (char ch : pin) {
int digit = ch - '0';
int encryptedDigit = (digit + 3) % 10;
encryptedPIN += char(encryptedDigit + '0');
}
return encryptedPIN;
}
string decryptPIN(const string& encryptedPIN) {
string decryptedPIN = "";
for (char ch : encryptedPIN) {
int digit = ch - '0';
int decryptedDigit = (digit - 3 + 10) % 10;
decryptedPIN += char(decryptedDigit + '0');
}
return decryptedPIN;
}
bool pinFileExists() {
ifstream file(FILE_NAME);
return file.good();
}
void setupPIN() {
string pin;
cout << "\nCreate a 4-digit PIN: ";
cin >> pin;
while (!isValidPIN(pin)) {
cout << "Invalid PIN. PIN must be exactly 4 digits.\n";
cout << "Create a 4-digit PIN: ";
cin >> pin;
}
string encryptedPIN = encryptPIN(pin);
ofstream file(FILE_NAME);
if (!file) {
cout << "Error: Could not open file for writing.\n";
return;
}
file << encryptedPIN;
file.close();
cout << "PIN setup successful.\n";
cout << "Your PIN has been encrypted and saved.\n";
}
bool login() {
if (!pinFileExists()) {
cout << "\nNo PIN has been set up yet. Please set up a PIN first.\n";
return false;
}
ifstream file(FILE_NAME);
if (!file) {
cout << "Error: Could not open credentials file.\n";
return false;
}
string storedEncryptedPIN;
file >> storedEncryptedPIN;
file.close();
string storedPIN = decryptPIN(storedEncryptedPIN);
const int MAX_ATTEMPTS = 3;
string enteredPIN;
for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
cout << "\nEnter your 4-digit PIN: ";
cin >> enteredPIN;
if (!isValidPIN(enteredPIN)) {
cout << "Invalid input. PIN must be exactly 4 digits.\n";
}
else if (enteredPIN == storedPIN) {
return true;
}
else {
cout << "Incorrect PIN.\n";
}
cout << "Attempts remaining: " << MAX_ATTEMPTS - attempt << endl;
}
return false;
}