user data security

Pages: 12
Nov 12, 2015 at 6:38am
Hello Friends,
I'm new to C++ coding and trying to built a small program which will ask user to login with the specific username and password and after successful login, user get the options of writing a new file or opens any previously written file and also gets option to encrypt his file.

I also want that if the person has entered wrong password thrice, then the files written previously through the program should get encrypted and decryption will be done later after verification of the user.

I have developed the first half i.e. login and reading and writing of file, and getting no clue about how to implement the remaining half.

PLEASE GUIDE ME ON HOW CAN I MAKE THE REST HALF and also I'm getting few errors in the code I've made, it in not working properly....
Please review this code

NOTE :: I have a compulsion of making this program in turbo c++ only, so please help me out.

Here is my complete code that i have developed


Thank you very much in advance, I will highly appreciate your help

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
  #include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <fstream.h>




void lockfolder();

int main()
{


	int i,a=0,count=0;
	char c,fname[10], mycode[12]="grantaccess";
	char USERNAME[11] = "miniproject";
	char PASSWORD[10] = "qwert!@#$%";
	unsigned int choice=0, lock, yes, code;

	char username[11], password[10];
	cout << "\n\t\t\t\t\tWelcome to our Security System..!!!" << endl;

	while(count<3)

	{

	cout << "\n\t\t\t\t\tEnter Username :: ";
	cin >> username;


	if(sizeof(username) < 11)
	{

		cout << "\t\t\t\t\tUsername too short.";
		count++;

	}

	else

	{

		cout << "\t\t\t\t\tEnter Password :: ";
		cin >> password;

		if(sizeof(password) < 10)
		{

			cout << "\t\t\t\t\tNot proper password.";
			count++;

		}

	}
	if(username == USERNAME && password == PASSWORD)
			{

				cout << "\n\n\t\t\t\t\t\tLogin Successful" << endl;
				cout << "\t\t\t\t\t\t\t-------------------\n\n" << endl;
				cout << "\n\n\t\t\t\t\t\t --=Welcome to the software=--" << endl;
				cout << "\t\t\t\t\t\t=================================\n\n\n" << endl;
				count=3;


				do
				{


				switch(a)

				{


					case 0:
					{


			cout << "Available commands:\n\n";
			cout << "0. list commands\n\n";
			cout << "1. write a new file\n\n";
			cout << "2. load a file\n\n";
			cout << "3. exit program\n\n";

			break;

			}



				case 1:
				{

					ofstream out;
			cout<<"Enter File name:";
			cin>>fname;
			out.open(fname);

					cout<<"Enter contents to store in file (Enter # at end):\n";
			while((c=getchar())!='#')
			{

			    out<<c;

					}
					cout<<"want to lock this file? press 1 else press 2 :: ";
					cin>>choice;

					if(choice==1)
					{

					lock=1;

					}

					else
					{

						cout<<"quitting...";

					}


			out.close();

				}

				case 2:
				{
					cout<<"enter file name to open ";
					cin>>fname;
					if(lock==1)
					{

					cout<<"sorry..this file is locked.\n Want to unlock? press 1 ";
					cin>>yes;

					if(yes==1)
					{

					cout<<"enter unlock code ";
					cin>>code;

					if(code==mycode[12])
					{

					lock=0;

					}
					else

					cout<<"wrong code";

					return 0;

					}
					else

					return 0;
					}

					char line[1000];
					cout<<"Enter File name: ";
					cin>>fname;
					ifstream myfile(fname);

					while (myfile.eof())
					{




					cout << line << '\n';



					myfile.close();

					}

					//else cout << "Unable to open file";
				
				}
				
				case 3:
				{
					
				 cout<< "\n\nTHANK YOU FOR USING THIS SOFTWARE...";
				// exit (0);
				
				}
				
				
				default:
            			
					cout << "\n\nUnrecognized command. Enter '0' to view command list\n";
				
				
			}
			
			
			cout << "\nEnter command:";
		cin >> a;

        }
        
        while (a!=3);
		}
	
				
			else
			{
				
				cout << "\n\t\t\t\t\tInvalid login details. Please try again"  
				<< endl;
				count++;
				
			}
			
		}
		
	}
	
	





Nov 12, 2015 at 9:20am
closed account (48T7M4Gy)
I have a compulsion of making this program in turbo c++ only

Noted.
Nov 12, 2015 at 10:18am
Let me point out some errors:

1
2
3
4
5
6
7
char USERNAME[11] = "miniproject";

 error C2117: 'USERNAME' : array bounds overflow

char PASSWORD[10] = "qwert!@#$%";

error C2117: 'PASSWORD' : array bounds overflow

Please remember that string are terminated with '\0' so you need to define the length one longer.
Also it would be better to use a type of constant for the length like

1
2
#define USER_LEN  12
#define PW_LEN     12 


Next errors are:

1
2
if(sizeof(username) < 11)
if(sizeof(password) < 10)


sizeof gives you the len of the var, not the len of the input - to get the len of the input you need the function strlen()

 
if(username == USERNAME && password == PASSWORD)


to compare strings you need the function strcmp() or stricmp()

Hope this helps.
Nov 12, 2015 at 11:16am
@Thomas1966

If I'm using any function like strcmp() or stricmp() in turbo c++, it is showing error as "strcmp() should have a prototype" and same error in stricmp also.

and also, program is never getting inside if condition, either it is not comparing the strings and directly displaying the switch case otherwise, it is getting inside else loop every time
Nov 12, 2015 at 12:07pm
You need to include <string.h> to use strcmp() or stricmp(). Could you post the code again once you have done it. There might be other errors as well.
Nov 12, 2015 at 2:07pm
oh, thank you Thomas1965, the errors are removed and the login part is working well now.

but the load a new file method is not working as it should work.

this is my complete program

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
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <fstream.h>




void lockfolder();

int main()
{


	int i,a=0,count=0;
	char c,fname[10], mycode[12]="grantaccess";
	char user[12] = "miniproject";
	char pass[11] = "qwert!@#$%";
	unsigned int choice=0, lock, yes, code;

	char username[11], password[10];
	cout << "\n\t\t\t\t\tWelcome to our Security System..!!!" << endl;

	while(count<3)

	{

	cout << "\n\t\t\t\t\tUsername :: ";
	cin >> username;

	cout << "\n\t\t\t\t\tPassword :: ";
	cin >> password;

	if(strlen(username) < 11)
		{
			cout << "\n\t\t\t\t\tnot proper username";
			count++;
		}

	if(strlen(password) < 10)
		{
			cout << "\n\t\t\t\t\tpassword not proper";
			count++;
		}

	if(strcmp(username, user)==0 && strcmp(password, pass)==0)
		{



				cout << "\n\n\t\t\t\t\t\tLogin Successful" << endl;
				cout << "\t\t\t\t\t\t\t-------------------\n\n" << endl;
				cout << "\n\n\t\t\t\t\t\t --=Welcome to the software=--" << endl;
				cout << "\t\t\t\t\t\t=================================\n\n\n" << endl;
				count=3;


				do
				{


				switch(a)

				{


			case 0:
			{


			cout << "Available commands:\n\n";
			cout << "0. list commands\n\n";
			cout << "1. write a new file\n\n";
			cout << "2. load a file\n\n";
			cout << "3. exit program\n\n";

			break;

			}



				case 1:
				{

					ofstream out;
			cout<<"Enter File name:";
			cin>>fname;
			out.open(fname);

					cout<<"Enter contents to store in file (Enter # at end):\n";
			while((c=getchar())!='#')
			{

			    out<<c;

					}
					cout<<"want to lock this file? press 1 else press 2 :: ";
					cin>>choice;

					if(choice==1)
					{

					lock=1;

					}

					else
					{

						cout<<"quitting...";

					}


			out.close();

				}

				case 2:
				{
					cout<<"enter file name to open ";
					cin>>fname;
					if(lock==1)
					{

					cout<<"sorry..this file is locked.\n Want to unlock? press 1 ";
					cin>>yes;

					if(yes==1)
					{

					cout<<"enter unlock code ";
					cin>>code;

					if(code==mycode[12])
					{

					lock=0;

					}
					else

					cout<<"wrong code";

					return 0;

					}
					else

					return 0;
					}

					char line[1000];
					cout<<"Enter File name: ";
					cin>>fname;
					ifstream myfile(fname);

					while (myfile.eof())
					{




					cout << line << '\n';



					myfile.close();

					}



				}

				case 3:
				{

				 cout<< "\n\nTHANK YOU FOR USING THIS SOFTWARE...";


				}


				default:

					cout << "\n\nUnrecognized command. Enter '0' to view command list\n";


			}


			cout << "\nEnter command:";
		cin >> a;

	}

	while (a!=3);
		}


			else
			{

				cout << "\n\t\t\t\t\tInvalid login details. Please try again"
				<< endl;
				count++;

			}

		}
	 return 0;
	}







Nov 12, 2015 at 2:55pm
The logic of your code would be much easier to follow - for you and us - if you adopted a sensible indentation policy.

Also, do you really think
not working as it should work
is a helpful description? How is it working? How does that differ from how it should work? Help us to help you.

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
        case 2:
        {
          cout<<"enter file name to open ";
          cin>>fname;
          if(lock==1)
          {
            cout<<"sorry..this file is locked.\n Want to unlock? press 1 ";
            cin>>yes;

            if(yes==1)
            {
              cout<<"enter unlock code ";
              cin>>code;

              if(code==mycode[12])
              {
                lock=0;
              }
              else
                cout<<"wrong code";

              return 0;
            }
            else
              return 0;
          }

          char line[1000];
          cout<<"Enter File name: ";
          cin>>fname;
          ifstream myfile(fname);

          while (myfile.eof())
          {
            cout << line << '\n';
            myfile.close();
          }
        }


EDIT: One thing that becomes easier to see with a sensible layout, is that if the file is locked, the function will exit without opening and reading the file, regardless of whether the user enters 1 or not.
Last edited on Nov 12, 2015 at 2:57pm
Nov 12, 2015 at 4:26pm
sorry that's my fault @MikeyBoy

actually, i want it to read and write files after user login (successful login) and also to hide data written in files if user wants i.e. after complete writing of file, if user want to encrypt the data then he could.

and if in case user fails to login correctly for consecutively 3 times, it must encrypt ll the files written via this program should get encrypted and decryption will be done after verification.


presently, it is doing the login process successfully and even writing the file as required but while it comes to load any file, the process terminates (even the file is not locked) and the available commands list is displayed again and it asks for user to input command to weather to write or load a file or to exit the program.


and talking about locking of any file, once the programs exits and you starts it again, lock value again gets to 0 i.e. all locked files are unlocked now, tough, later on, i will not use this lock method instead i will ask user to encrypt the file or not.
Nov 12, 2015 at 4:43pm
So what actually happens when the user selects "2" in the main menu?

Do you see the prompt:

enter file name to open

or any of the other expected prompts?
Last edited on Nov 12, 2015 at 4:51pm
Nov 12, 2015 at 4:53pm
ya, it do show this message

 
enter file name to open


but actually, when I'm giving input as 1, it is writing file and after saving the file as per the given name, case 2 and case 3 are automatically starting and all the cout << "" are displayed by default
Nov 12, 2015 at 4:57pm
if user select 1, case 2 and 3 also gets starts after case 1 ends and if user selects 2, case 3 starts after ending case 2, the similar process is continuing
Nov 12, 2015 at 5:24pm
Oh, that's because you've forgotten to put break statements at the end of each case.
Nov 13, 2015 at 4:11am
ya, it's fine now but why this part of code is not working?

1
2
3
4
5
6
7
8
9
10
11

char line[1000];
cout<<"Enter File name: ";
cin>>fname;
ifstream myfile(fname);
while (myfile.eof())
{
cout << line << '\n';
myfile.close();
}
Nov 13, 2015 at 8:46am
It must be !myfile.eof() otherwise it will never be true.
Nov 13, 2015 at 11:55am
it's not working even if !myfile.eof() is used.

after this command !myfile.eof() if I try to load any file, a blank black screen is displayed and turbo c++ gets hang.
Nov 13, 2015 at 12:01pm
cout << line << '\n';

you need to get the input into line otherwise it is empty.
Nov 14, 2015 at 4:16am
but I'm doing the same I guess

and also this code is working well in Dev C++, but not in Turbo C++
Nov 14, 2015 at 10:14am
Do I understand right that you just want to display the file?

If yes you can do it like this;

1
2
3
4
5
6
string input;
ifstream myfile(fname);
while (myfile >> input)
{
   cout << input << '\n';
}
Nov 14, 2015 at 10:32am
but I'm doing the same I guess

You're not reading what Thomas is telling you:

you need to get the input into line otherwise it is empty.

Nowhere in your code do you store anything in line. line is a completely empty char array.
Last edited on Nov 14, 2015 at 10:32am
Nov 14, 2015 at 1:55pm
@Thomas, sorry your given code is not working i mean, content of the text file is not displayed


@MikeyBoy, even if I'm putting the input in line, content is not being displayed
Pages: 12