Please Help ASAP

Iset it up so that the key for the encrypting is randomized and stored into a separate file with my intentions of it being like a key fob. So my program will only work with one user at a time unless I set it up to direct to a drive letter where a usb could be read in.

However, the two issues im having is even if i use the username whose key is stored into file private.txt, the program grants access the first time but if i try to access it a second time it fails.

I've also lost about half my hair trying to figure out a naming convention aspect for the program so that duplicate users cannot be created. Ive tried bringing in the file and converting it into as a string array and increment the position but its not working for me, im probably doing something really dumb to mess it up. I attached my source code so far. any help would be greatly appreciated.

I am a complete BEGINNER, so please K.I.S.S. cause im the last S when it comes to this.

The encryption is basically ROT-X.
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <iostream>
#include <string>
#include <fstream> 
#include <algorithm>
#include <cstdlib>


using namespace std;



void createUser();
void userLogin();
void keyFob(string);
string encrypt(string);
string decrypt(string);
string duplicate(string);

ifstream file("userCred.txt");
ofstream created("userCred.txt", ios::in | ios::out | ios::app);
ifstream keyFile("private.txt");


int main()
{
					char choice;
					char create = '1';
					char login = '2';
					char exit = '3';
			


	
					do {

							do {
								cout << endl << endl;
								cout << "***** ACCOUNT ACCESS *****" << endl << endl;
								cout << "1) Create New Account" << endl;
								cout << "2) Login to Existing Account" << endl;
								cout << "3) Exit " << endl << endl;
								cout << "Please Enter Your Choice: ";
								cin >> choice;


							} while (choice != create && choice != login && choice != exit);
		

											switch (choice) {

											case '1':
												createUser();
												break;

											case '2':
												userLogin();
												break;

											default:
												return 0;

											}

					} while (choice == '1' || choice == '2');

}

void createUser() {

								//This is declaring all the strings that will be needed to create the user

								string fname;
								string lname;
								string username;
								string password;
								string email;
								string check;
								string yes = "y";
								string yes2 = "Y";
								string no = "n";
								string no2 = "N";
								char a;
								int pwdCheck;

								//This declares where the new users credentials will be stored.
								cout << endl << endl;
								cout << "New User Account Creation" << endl;
								cout << endl;

								//This code queries the user and verifies the information
								do
								{
									cout << "Please Enter Your First Name: ";
									cin.ignore();
									getline(cin, fname);
									cout << "Please Enter Your Last Name: ";
									getline(cin, lname);
									cout << endl;
									cout << "Please Verify That Your Name Is: " << fname << " " << lname << endl;

									// This code verifies that the user has entered a the correct information.
									do
									{
										cout << endl;
										cout << "Is This Information Correct? Please ENTER (Y for Yes or N for No): ";
										getline(cin, check);
									} while ((check != yes) && (check != yes2) && (check != no) && (check != no2));

									cout << endl;

								} while ((check != yes) && (check != yes2));

								// This code will store the password and verify that it meets the length requirement and that there are no empty spaces
								// If it doesnt meet these requirements it will reprompt

								do {
									cout << "Please Enter a Password for the Created User: ";

									do {
										getline(cin, password);
									} while (0 == password.length());

									pwdCheck = password.find(' ');

									if (pwdCheck > 0) {
										cout << "Spaces are not permitted in your password, please try again." << endl;
										cout << endl;
									}
									if (10 > password.length()) {
										cout << "Passwords must be atleast 10 characters in length" << endl;
										cout << endl;
									}

								} while (pwdCheck > 0 || 10 > password.length());
				keyFob(password);

				password = encrypt(password);

								cout << "This is the encrypted password " << password << endl;

								//This will take the entered information and create the username in lowercase
								username = lname.substr(0, 4) += fname.at(0);

				duplicate(username);
								cout << "This is the username when created: " << username << endl;

								for (int i = 0; i < username.length(); i++) {
									username[i] = tolower(username[i]);

								}

								// This will check for duplicate usernames and adjust

								//This creates the email account and outputs the generated email for the user

								email = username + "@fbi.gov";

								cout << endl << endl;
								cout << "Account has been created for: " << username << endl;
								cout << "Email Address: " << email << endl;
								// This will write the information to the credentials file for storage

								created << username + "::" + password + "::" + email << endl;
								created.close();
								return;
 }


//This function checks for duplicate usernames within the credentials file
string duplicate(string username) {


}





void userLogin() {
	
							string username;
							string password;
							string credCopy;
							string creds;
							int check;
							int credFile;
							int safety = 0;
							bool authorized = false;

							//This streams the credentials file into a string named transfer 

							string transfer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
							credCopy = string(transfer);

							do {
								// This will prompt the user for their username and password and store it for comparison
								cout << endl;
								cout << "Please enter your username: ";
								cin.ignore();
								getline(cin, username);
								cout << endl;
								cout << "Please enter your password: ";
								getline(cin, password);
								cout << endl;

								password = encrypt(password);

								//Creating credentials string and storing it as string creds

								creds = username + "::" + password;

								//Finding entered credentials location within the string

								check = credCopy.find(creds);
								credFile = credCopy.length();

								//Granting or Denying Access to the user
								if (check >= 0 && check < credFile) {

									cout << endl << endl;
									cout << "          Access Granted" << endl;
									cout << endl;
									cout << "          Welcome " << username << "!" << endl;
									cout << endl;
									authorized = true;
									safety = 10;
									
								}
								else {

									if (safety < 2) {
										cout << "The Username/Password combination could not be verified, please try again." << endl;
										cout << endl << endl;
										safety++;
									}

									else {
										safety++;
									}

								}



							} while ((authorized == false) && (safety < 3));

							if (safety == 3) {

								cout << "This terminal has been locked for security purposes, please contact your network administrator" << endl << endl;
							}


							return;

	
}

string encrypt(string password) {

							string encrypted;


							string keys((std::istreambuf_iterator<char>(keyFile)), std::istreambuf_iterator<char>());
							int key = atoi(keys.c_str());

							cout << " Password before encryption : " << password << endl;
							for (int i = 0; i <= password.length(); i++) {

								if (password[i] >= '!') {
									if (password[i] == 'z') {
										password[i] = '!';
									}
								}
								password[i] += key;

							}

							encrypted = password;
							cout << " Password after encryption: " << encrypted << endl;
							return encrypted;
}

void keyFob(string password) {
							ofstream keyCreate("private.txt");

							srand(password.length());
							int keyVal = ((rand() % 20) + (rand()% 30));

							keyCreate << keyVal;
							keyCreate.close();
	
							return;
}
Last edited on
Topic archived. No new replies allowed.