-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_structure.c
1020 lines (929 loc) · 30.1 KB
/
user_structure.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Color Codes for teminal ANSI
*/
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
//Regular bold text
#define BBLK "\e[1;30m"
#define BRED "\e[1;31m"
#define BGRN "\e[1;32m"
#define BYEL "\e[1;33m"
#define BBLU "\e[1;34m"
#define BMAG "\e[1;35m"
#define BCYN "\e[1;36m"
#define BWHT "\e[1;37m"
//Regular underline text
#define UBLK "\e[4;30m"
#define URED "\e[4;31m"
#define UGRN "\e[4;32m"
#define UYEL "\e[4;33m"
#define UBLU "\e[4;34m"
#define UMAG "\e[4;35m"
#define UCYN "\e[4;36m"
#define UWHT "\e[4;37m"
//High intensty background
#define BLKHB "\e[0;100m"
#define REDHB "\e[0;101m"
#define GRNHB "\e[0;102m"
#define YELHB "\e[0;103m"
#define BLUHB "\e[0;104m"
#define MAGHB "\e[0;105m"
#define CYNHB "\e[0;106m"
#define WHTHB "\e[0;107m"
//Regular background
#define BLKB "\e[40m"
#define REDB "\e[41m"
#define GRNB "\e[42m"
#define YELB "\e[43m"
#define BLUB "\e[44m"
#define MAGB "\e[45m"
#define CYNB "\e[46m"
#define WHTB "\e[47m"
#define reset "\e[0m"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>
#include "header.h"
// void print_home_screen();
// void print_password_screen(char* , char*);
/*
This function is to auto increment userID for every new user created so that everyone has a unique ID.
Implementation?
Read the last user id from usercount file in admin directory and update it after
incrementing 1.
Return this new value to the create_user Function
*/
int calculate_user_id()
{
FILE *fp2;
fp2 = fopen("Admin/usercount", "r+");
int count;
fscanf (fp2, "%d", &count);
fp2=freopen(NULL,"w",fp2);
fprintf(fp2, "%d", count+1);
fclose(fp2);
return count+1;
}
/*
This function creates the object(or struct) of every new user created.
* We check if a user exists by the given name.
* New userID is generated by int calculate_user_id() function
* if both are unique a new user is created and this data is then used in print_new_user_interface function.
* the code is generated by the current system time. Unknown to the user themselves.
* Other fields are added for future application.
*/
user* create_user(char usrnm[], char pswd[]) {
time_t t;
t = time(NULL);
user* obj = malloc(sizeof(user));
obj->user_id = calculate_user_id();
strcpy(obj->username,usrnm);
strcpy(obj->password, pswd);
obj-> code = t%100;
obj-> code2 = 45;
obj-> isloggedin = 0;
return obj;
}
/*
This is a utility function used multiple times in other functions to check if a user already exists.
* We check if a user exists by the given name.
* return 1 if yes. 0 if not.
*/
int exists(char *fname)
{
FILE *file;
if ((file = fopen(fname, "r")))
{
fclose(file);
return 1;
}
return 0;
}
/*
This function creates the file of every new user created.
* The new user data is fetched through the struct and passed to this function by the print_new_user_inteface function.
* All the data is written in a file. in the Users directory.
* Functions asks to press any key followed by return key to go back to home screen.
*/
void create_user_file(user* obj)
{
FILE *fp1,*fp2;
char location[100] = "Users/";
strcat(location, obj->username);
strcat(location, ".txt");
if(exists(location))
{
printf(ANSI_COLOR_RED "USER ALREADY EXISTS" ANSI_COLOR_BLUE "\nPress any key to go back to home screen\n");
}
else{
fp1 = fopen(location , "w+");
fprintf(fp1, "%d", obj->user_id);
fputc('\n', fp1);
fputs(obj->username, fp1);
fputc('\n', fp1);
fputs(obj->password, fp1);
fputc('\n', fp1);
fprintf(fp1, "%d", obj->code);
fputc('\n', fp1);
fclose (fp1);
printf("USER CREATED SUCCESSFULY\nPress any key to go back to home screen" ANSI_COLOR_RESET "\n");
// fwrite (obj, sizeof(user), 1, fp1);
}
int newinput;
scanf("%d",&newinput);
switch (newinput)
{
default:
print_home_screen();
}
// enter_string_to_file(fp1, obj->username);
}
/*
This function is responsible for logging the user in.
* We check if a user exists by the given name.
* If so, we print basic data i.e the name and ID of that user
* The next input demands a user to enter their password.
* If the entered password matches the user password we log-in the user and call the print_post_login_interface function
* else we keep asking for the correct password or for 'exit' key.
*/
void read_user_file(char *username)
{
char* arr[] = {"User_id", "Username", "Password"};
FILE *fp1,*fp2;
char location[100] = "Users/";
strcat(location, username);
strcat(location, ".txt");
if(!exists(location))
{
printf("USER DOES NOT EXIST");
printf("\nPress any key to go back to home screen" ANSI_COLOR_RESET "\n");
int newinput;
scanf("%d",&newinput);
switch (newinput)
{
default:
print_home_screen();
}
}
else{
fp1 = fopen (location, "r");
char nextline[10000];
int f=0;
while (fgets(nextline, sizeof(nextline), fp1) && f<2) {
printf(ANSI_COLOR_MAGENTA "%s : %s" ANSI_COLOR_RESET, arr[f], nextline);
f++;
}
char password[100];
strcpy(password , nextline);
fclose (fp1);
printf(ANSI_COLOR_YELLOW "Enter Password: (or type 'exit' to go back to main menu)" ANSI_COLOR_RESET);
char entered_password[100];
scanf("%s", entered_password);
print_password_screen(password, entered_password, location);
// printf ("File exists");
}
}
/*
This function is used as a utility function by the read_user_file function.
* We prompt the user to enter a password until a correct password or exit is entered.
* If the entered password matches the user password we log-in the user and call the print_post_login_interface function
* else we keep asking for the correct password or for 'exit' key.
*/
void print_password_screen(char* password, char* entered_password, char location[])
{
// printf("%s%s", password, entered_password);
// printf("%d", strcmp(password, entered_password));
if(strcmp("exit", entered_password) != 0)
{
if((strcmp(password, strcat(entered_password,"\n")) == 0) || (strcmp(password, entered_password) == 0))
{
//Logged in interface
printf("LOGGED IN SUCC");
print_post_login_interface(location);
}
else
{
printf(ANSI_COLOR_YELLOW "Incorrect Password - Try Again or type 'exit' to go back to main menu: " ANSI_COLOR_RESET);
scanf("%s", entered_password);
print_password_screen(password, entered_password, location);
}
}
else
{
print_home_screen();
}
}
/*
This function created a new user struct and then creates a file according to the functions defined above
*/
void print_new_user_interface()
{
system("clear");
char uname[100], pswd[100];
printf(ANSI_COLOR_GREEN "-----------------------------------------------\n");
printf("| Enter Username : ");
scanf("%s",uname);
printf("| Enter Password : ");
scanf("%s",pswd);
printf("|\n" ANSI_COLOR_BLUE);
user* obj = create_user(uname,pswd);
create_user_file(obj);
}
/*
This function is responsible for printing our home screen
* This is the callback route for every function
* In case a user enters 'exit' in the password feild or logs out if they were previously logged in. this is called.
*/
void print_home_screen()
{
system("clear");
printf(ANSI_COLOR_GREEN "-----------------------------------------------\n");
printf("| Press 1 to create a new user |\n");
printf("| Press 2 to login |\n");
printf("| Press 0 to exit |\n\n\n");
int input = 1;
printf("Choose an Option ");
while(input !=0)
{
scanf("%d", &input);
switch (input)
{
case 0:
return;
break;
case 1:
print_new_user_interface();
return;
break;
case 2:
printf(ANSI_COLOR_YELLOW "Enter the name of the user\n" ANSI_COLOR_RESET);
char uname[100];
scanf("%s",uname);
read_user_file(uname);
return;
break;
default:
printf(ANSI_COLOR_CYAN "Enter Valid Input : " ANSI_COLOR_RESET);
break;
}
}
}
/*
* Switch Case is used to ask user for different routes.
* A user is allowed to change their password.
* To encode or decode a file
* or multiple files at once(Not implemented yet)
*/
void print_post_login_interface(char* location)
{
char copy[100];
strcpy(copy, location);
system("clear");
printf(ANSI_COLOR_CYAN "-----------------------------------------------\n");
printf("| ----------Logged in with user----------- |\n");
printf("| ----------"ANSI_COLOR_RED " %s " ANSI_COLOR_CYAN"----------- |\n", user_locationstr_to_user(copy+6));
printf("| Press 1 to change password |\n");
printf("| Press 2 to encrypt individual file |\n");
printf("| Press 3 to encrypt multiple files |\n");
// printf("| Press 4 to encrypt all files in directory |\n");
printf("| Press 5 to decrypt individual file |\n");
printf("| Press 6 to decrypt multiple files |\n");
// printf("| Press 7 to decrypt all files in directory |\n");
printf("| Press 8 to logout and return to main menu |\n");
int input = 1;
printf(ANSI_COLOR_YELLOW "Choose an Option : " ANSI_COLOR_RESET);
scanf("%d", &input);
switch (input)
{
case 1:
print_edit_user_interface(location);
break;
case 2:
print_encryptfile_interface(location, 0);
break;
case 5:
print_decryptfile_interface(location, 0);
break;
case 8:
print_home_screen();
break;
// case 1:
// print_new_user_interface();
// return;
// break;
// case 2:
// printf(ANSI_COLOR_YELLOW "Enter the name of the user\n" ANSI_COLOR_RESET);
// char uname[100];
// scanf("%s",uname);
// read_user_file(uname);
// return;
// break;
default:
printf(ANSI_COLOR_CYAN "Enter Valid Input : " ANSI_COLOR_RESET);
break;
}
}
/*
* This allows a user to change thier password.
* We create a temporary file that copies all the data of a user
* Apart from the password line all the data is copied as is it. and the new password replaces
* the old one.
*/
void print_edit_user_interface(char* location)
{
system("clear");
printf(MAGB "Enter New Password\n" reset);
char newpwd[100];
scanf("%s", newpwd);
strcat(newpwd, "\n");
FILE* fp1 = fopen (location, "r+");
FILE* fp2 = fopen ("Users/temp.txt", "w+");
char nextline[10000];
int f=0;
while (fgets(nextline, sizeof(nextline), fp1)) {
f++;
if(f==3)
{
fputs(newpwd, fp2);
continue;
}
fputs(nextline, fp2);
}
remove(location);
rename("Users/temp.txt", location);
fclose(fp1);
fclose(fp2);
printf(BGRN "Your Password Was Changed" reset);
printf("\nPress any key to go back to user home \n");
int newinput;
scanf("%d",&newinput);
switch (newinput)
{
default:
print_post_login_interface(location);
}
}
/*
* Ask the user to enter the path of any text file.
* Checks if the path is valid. If not repeats the process until a valid path is entered or the user enters 'exit'
* If a valid path is found, we send the location and fetch the code from user file and send this data
* to the encrypt1 function
*/
void print_encryptfile_interface(char* location, int errcode)
{
system("clear");
if(errcode == 1)
{
printf(REDB "No file found at provided location\n" reset);
}
printf(BLUB "Enter path of file to be encrypted or 'exit' to go back to user home\n" reset);
char filepath[1000];
scanf("%s", filepath);
if(strcmp("exit", filepath) == 0)
{
print_post_login_interface(location);
}
else if(!exists(filepath))
{
print_encryptfile_interface(location, 1);
}
else
{
FILE* fp1 = fopen (location, "r");
char nextline[10000];
int f=0;
while (fgets(nextline, sizeof(nextline), fp1) && f<3) {
f++;
}
char codestr[100];
strcpy(codestr , nextline);
int code = atoi(codestr);
fclose (fp1);
encrypt1(location, filepath, code);
}
// printf("\nPress any key to go back to user home \n");
// int newinput;
// scanf("%d",&newinput);
// switch (newinput)
// {
// default:
// print_post_login_interface(location);
// }
}
/*
* Ask the user to enter the path of any text file.
* Checks if the path is valid. If not repeats the process until a valid path is entered or the user enters 'exit'
* If a valid path is found, we send the location and fetch the code from user file and send this data
* to the decrypt1 function
*/
void print_decryptfile_interface(char* location, int errcode)
{
system("clear");
if(errcode == 1)
{
printf(REDB "No file found at provided location\n" reset);
}
printf(BLUB "Enter path of file to be decrypted or 'exit' to go back to user home\n" reset);
char filepath[1000];
scanf("%s", filepath);
if(strcmp("exit", filepath) == 0)
{
print_post_login_interface(location);
}
else if(!exists(filepath))
{
print_decryptfile_interface(location, 1);
}
else
{
FILE* fp1 = fopen (location, "r");
char nextline[10000];
int f=0;
while (fgets(nextline, sizeof(nextline), fp1) && f<3) {
f++;
}
char codestr[100];
strcpy(codestr , nextline);
int code = atoi(codestr);
fclose (fp1);
decrypt1(location, filepath, code);
}
// printf("\nPress any key to go back to user home \n");
// int newinput;
// scanf("%d",&newinput);
// switch (newinput)
// {
// default:
// print_post_login_interface(location);
// }
}
/*
* Checks if the file to be encrypted belongs to the user or some other user
* If it belongs to no one, then we encrypt the file and map it to the current user.
* If it belongs to some other user, then prints an error and returns to home.
* If the file belongs to the user already. Simply encrypt it.
*/
void encrypt1(char* location, char* filepath, int code)
{
int belong_code = belongs_to(location, filepath);
if(belong_code == 2)
{
//return
printf(ANSI_COLOR_RED "This file belongs to some other user.\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_YELLOW "Press any key to go back to user home \n" ANSI_COLOR_RESET);
int newinput;
scanf("%d",&newinput);
switch (newinput)
{
default:
print_post_login_interface(location);
}
}
else if(belong_code == 0 || belong_code == 1)
{
if(belong_code == 0)
{
//append
write_to_user(location, filepath);
}
char ch;
FILE *fps, *fpt;
fps = fopen(filepath, "r");
if(fps == NULL)
printf("\nFile not found\n");
fpt = fopen("tempfile.png", "w");
if(fpt == NULL)
printf("\nCouldn't create temp file\n");
else
{
printf(BRED "Creating encrypted temp file..\n" reset);
ch = fgetc(fps);
while(ch != EOF)
{
ch = ch+code;
fputc(ch, fpt);
ch = fgetc(fps);
}
fclose(fps);
fclose(fpt);
}
remove(filepath);
FILE* fps2,*fpt2;
fps2 = fopen(filepath, "w");
if(fps2 == NULL)
printf("\nFile not found\n");
fpt2 = fopen("tempfile.png", "r");
if(fpt2 == NULL)
printf("\nCouldn't read temp file\n");
else
{
ch = fgetc(fpt2);
while(ch != EOF)
{
ch = fputc(ch, fps2);
ch = fgetc(fpt2);
}
fclose(fps2);
fclose(fpt2);
}
remove("tempfile.png");
printf(BRED "Removing temp file..\n" reset);
printf(BRED "Finalising..\n" reset);
printf(BRED "File Encrypted Successfully!\n" reset);
printf(ANSI_COLOR_YELLOW "Press any key to go back to user home \n" ANSI_COLOR_RESET);
int newinput;
scanf("%d",&newinput);
switch (newinput)
{
default:
print_post_login_interface(location);
}
}
}
void encrypt2(char* location, char* filepath, int code)
{
int belong_code = belongs_to(location, filepath);
if(belong_code == 2)
{
//return
printf(ANSI_COLOR_RED "This file belongs to some other user.\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_YELLOW "Press any key to go back to user home \n" ANSI_COLOR_RESET);
int newinput;
scanf("%d",&newinput);
switch (newinput)
{
default:
print_post_login_interface(location);
}
}
else if(belong_code == 0 || belong_code == 1)
{
if(belong_code == 0)
{
//append
write_to_user(location, filepath);
}
char ch;
FILE *fps, *fpt;
fps = fopen(filepath, "r");
if(fps == NULL)
printf("\nFile not found\n");
fpt = fopen("tempfile.png", "w");
if(fpt == NULL)
printf("\nCouldn't create temp file\n");
else
{
int i = 0;
int j = (code%10)+3;
printf(BRED "Creating encrypted temp file..\n" reset);
ch = fgetc(fps);
while(ch != EOF)
{
i++;
if(j%i==0)
ch = ch+code;
fputc(ch, fpt);
ch = fgetc(fps);
}
fclose(fps);
fclose(fpt);
}
remove(filepath);
FILE* fps2,*fpt2;
fps2 = fopen(filepath, "w");
if(fps2 == NULL)
printf("\nFile not found\n");
fpt2 = fopen("tempfile.png", "r");
if(fpt2 == NULL)
printf("\nCouldn't read temp file\n");
else
{
ch = fgetc(fpt2);
while(ch != EOF)
{
ch = fputc(ch, fps2);
ch = fgetc(fpt2);
}
fclose(fps2);
fclose(fpt2);
}
remove("tempfile.png");
printf(BRED "Removing temp file..\n" reset);
printf(BRED "Finalising..\n" reset);
printf(BRED "File Encrypted Successfully!\n" reset);
printf(ANSI_COLOR_YELLOW "Press any key to go back to user home \n" ANSI_COLOR_RESET);
int newinput;
scanf("%d",&newinput);
switch (newinput)
{
default:
print_post_login_interface(location);
}
}
}
/*
* Checks if the file to be decrypted belongs to the user or some other user
* If it belongs to no one or
* If it belongs to some other user, then prints an error and returns to home.
* If the file belongs to the user already. Simply decrypt it.
*/
void decrypt1(char* location, char* filepath, int code)
{
int belong_code = belongs_to(location, filepath);
if(belong_code == 2 || belong_code == 0)
{
//return
printf(ANSI_COLOR_RED "This file belongs to some other user.\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_YELLOW "Press any key to go back to user home \n" ANSI_COLOR_RESET);
int newinput;
scanf("%d",&newinput);
switch (newinput)
{
default:
print_post_login_interface(location);
}
}
else
{
char ch;
FILE *fps, *fpt;
fps = fopen(filepath, "r");
if(fps == NULL)
printf("\nFile not found\n");
fpt = fopen("tempfile.png", "w");
if(fpt == NULL)
printf("\nCouldn't create temp file\n");
else
{
printf(BGRN "Creating decrypted temp file..\n" reset);
ch = fgetc(fps);
while(ch != EOF)
{
ch = ch-code;
fputc(ch, fpt);
ch = fgetc(fps);
}
fclose(fps);
fclose(fpt);
}
remove(filepath);
fps = fopen(filepath, "w");
if(fps == NULL)
printf("\nFile not found\n");
fpt = fopen("tempfile.png", "r");
if(fpt == NULL)
printf("\nCouldn't read temp file\n");
else
{
ch = fgetc(fpt);
while(ch != EOF)
{
ch = fputc(ch, fps);
ch = fgetc(fpt);
}
printf(BGRN "Removing temp file..\n" reset);
fclose(fps);
fclose(fpt);
remove("tempfile.png");
}
printf(BGRN "Finalising..\n" reset);
printf(BGRN "File Decrypted Successfully!\n" reset);
printf(ANSI_COLOR_YELLOW "Press any key to go back to user home \n" ANSI_COLOR_RESET);
int newinput;
scanf("%d",&newinput);
switch (newinput)
{
default:
print_post_login_interface(location);
}
}
}
void decrypt2(char* location, char* filepath, int code)
{
int belong_code = belongs_to(location, filepath);
if(belong_code == 2 || belong_code == 0)
{
//return
printf(ANSI_COLOR_RED "This file belongs to some other user.\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_YELLOW "Press any key to go back to user home \n" ANSI_COLOR_RESET);
int newinput;
scanf("%d",&newinput);
switch (newinput)
{
default:
print_post_login_interface(location);
}
}
else
{
char ch;
FILE *fps, *fpt;
fps = fopen(filepath, "r");
if(fps == NULL)
printf("\nFile not found\n");
fpt = fopen("tempfile.png", "w");
if(fpt == NULL)
printf("\nCouldn't create temp file\n");
else
{
int i = 0;
int j = (code%10)+3;
printf(BGRN "Creating decrypted temp file..\n" reset);
ch = fgetc(fps);
while(ch != EOF)
{
i++;
if(j%i==0)
ch = ch-code;
fputc(ch, fpt);
ch = fgetc(fps);
}
fclose(fps);
fclose(fpt);
}
remove(filepath);
fps = fopen(filepath, "w");
if(fps == NULL)
printf("\nFile not found\n");
fpt = fopen("tempfile.png", "r");
if(fpt == NULL)
printf("\nCouldn't read temp file\n");
else
{
ch = fgetc(fpt);
while(ch != EOF)
{
ch = fputc(ch, fps);
ch = fgetc(fpt);
}
printf(BGRN "Removing temp file..\n" reset);
fclose(fps);
fclose(fpt);
remove("tempfile.png");
}
printf(BGRN "Finalising..\n" reset);
printf(BGRN "File Decrypted Successfully!\n" reset);
printf(ANSI_COLOR_YELLOW "Press any key to go back to user home \n" ANSI_COLOR_RESET);
int newinput;
scanf("%d",&newinput);
switch (newinput)
{
default:
print_post_login_interface(location);
}
}
}
/*
* Takes a user file and path of a file as input
* Returns 0 If file belongs to no one
* Returns 1 If file belongs to current user
* Returns 2 If file belongs to some other user
*/
int belongs(char* location, char* filepath)
{
char tempfilepath[100];
strcpy(tempfilepath, filepath);
strcat(tempfilepath, "\n");
// printf("checking for user %s\n", location);
FILE* fp1 = fopen (location, "r");
char nextline[10000];
int f=0;
while (fgets(nextline, sizeof(nextline), fp1) && f<4) {
f++;
}
if(strcmp(nextline,"")!=0)
{
// printf("Nextline to be checked : %s\n with %s", nextline, tempfilepath);
if(strcmp(tempfilepath, nextline) == 0)
{
// printf("Returning 1");
return 1;
}
else
{
while (fgets(nextline, sizeof(nextline), fp1))
{
// printf("Nextline to be checked : %s\n with %s", nextline, tempfilepath);
if(strcmp(tempfilepath, nextline) == 0)
{
// printf("returning 1");
return 1;
}
}
}
// printf("NOT FOUND\n");
return 0;
}
return 0;
fclose (fp1);
}
/*
* Calls belong function for every user and returns the final key to the encrypt and decrypt functions
*/
int belongs_to(char* location, char* filepath)
{
DIR *d;
struct dirent *dir;
d = opendir("Users");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
char temploc[100] = "Users/";
strcat(temploc, dir->d_name);
if(strcmp(location, temploc) == 0)
{
if(belongs(temploc, filepath))
return 1;
}
else
{
if(belongs(temploc, filepath))
return 2;
}
}
closedir(d);
}
return 0;
}
/*
* If 0 is returned by belongs_to function. Then enters the path of the file
* to the current user and appends it to the end.
*/
void write_to_user(char* location, char* filepath)
{
FILE *fa = fopen(location, "a");
fputs(filepath, fa);
fputc('\n', fa);
fclose(fa);
}
/*
* Own implementation of fprintf()
*/
void enter_string_to_file(FILE* fp1, char str[]) {
int i=0;
while(str[i]!='\0')
{
fputc(str[i],fp1);
i++;
}
}
/*
* Own implementation of substr(left,right)
*/
char* user_locationstr_to_user(char* str)
{
str[strlen(str)-4] = '\0';
return str;
}
//WASTE UTILITY CODE
// int main(void)
// {
// print_home_screen();
// return 0;
// }
// int i , choice;
// FILE *fp1,*otpflptr;
// char oname[100];
// user det;
// int recsize;
// char c;
// if(fp1 == NULL)
// {
// fp1 = fopen("record.dat" , "w+");
// if(fp1 == NULL)
// {
// printf("error in opening file : \n");
// return -1;
// }
// }
// recsize = sizeof(det);
// fseek(fp1 , 0 ,SEEK_END);
// printf("Enter employee Name : ");
// scanf("%[^\n]" , det.name);
// printf("Enter roll number : ");
// scanf("%d" , &det.roll);
// printf("Enter the salary : ");
// scanf("%d" , &det.salary);
// scanf("%c" , &c);
// printf("Enter address : ");
// scanf("%[^\n]" , det.address);