-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogged.py
792 lines (733 loc) · 27.4 KB
/
Logged.py
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
import os
import pickle
import time
import sys
import shutil
from cryptography.fernet import Fernet
os.system('color 0A')
def dres():
if os.path.exists("C:\\Users\\Public\\Documents\\Logged Database"):
shutil.rmtree("C:\\Users\\Public\\Documents\\Logged Database")
def display(mem_key, log_key):
os.system('cls')
try:
key = load_key()
decrypt("C:\\Users\\Public\\Documents\\Logged Database\\Logged" + str(mem_key) + "dot" + str(log_key) + ".lg",
key)
except:
pass
with open("C:\\Users\\Public\\Documents\\Logged Database\\Logged" + str(mem_key) + "dot" + str(log_key) + ".lg",
"rb") as f:
os.system('cls')
print("\n", users[mem_key], "'s", logs[log_key], "as logged:\n")
ct = 1
pickle.load(f)
while 1:
try:
print("", ct, ": ", pickle.load(f), end="")
ct = ct + 1
except EOFError:
print("")
break
try:
key = load_key()
encrypt("C:\\Users\\Public\\Documents\\Logged Database\\Logged" + str(mem_key) + "dot" + str(log_key) + ".lg",
key)
except:
pass
return ct
def delete_line(original_file, line_number):
say = 0
key = load_key()
decrypt(original_file, key)
is_skipped = False
current_index = 0
dummy_file = original_file + '.bak'
with open(original_file, 'rb') as read_obj, open(dummy_file, 'wb') as write_obj:
for line in read_obj:
if current_index != line_number:
write_obj.write(line)
say = say+1
else:
is_skipped = True
current_index += 1
if say == 2:
os.remove(dummy_file)
os.remove(original_file)
return 27
elif is_skipped:
os.remove(original_file)
os.rename(dummy_file, original_file)
key = load_key()
encrypt(original_file, key)
else:
os.remove(dummy_file)
key = load_key()
encrypt(original_file, key)
return 0
def dellog(mem_key, log_key,):
while 1:
os.system('cls')
ct = display(mem_key, log_key)
print("\nEnter the index of the log that is to be deleted, 'n' to go back")
try:
i = int(input("-->"))
except:
os.system('cls')
return
if (i >= ct or i <= 0):
print("Wrong choice, please try again!\n")
continue
else:
try:
key = load_key()
decrypt("C:\\Users\\Public\\Documents\\Logged Database\\Logged" +
str(mem_key) + "dot" + str(log_key) + ".lg", key)
except:
pass
with open("C:\\Users\\Public\\Documents\\Logged Database\\Logged" + str(mem_key) + "dot" + str(log_key) + ".lg", "rb") as f:
c = 0
while 1:
try:
pickle.load(f)
c = c + 1
if c == i:
dell = {}
dell = pickle.load(f)
break
except EOFError:
print("")
break
try:
key = load_key()
encrypt("C:\\Users\\Public\\Documents\\Logged Database\\Logged" +
str(mem_key) + "dot" + str(log_key) + ".lg", key)
except:
pass
os.system('cls')
print("Selected log:", (i), ":", dell, "\n")
inch = input("To delete enter 'y' or 'n' to go back: ")
if inch != ('y' or 'Y'):
break
else:
suk = delete_line("C:\\Users\\Public\\Documents\\Logged Database\\Logged" + str(
mem_key) + "dot" + str(log_key) + ".lg", i)
if suk == 27:
os.system('cls')
print(users[mem_key] + "'s" + " entire log of " +
logs[log_key] + " successfully deleted!\n")
return
os.system('cls')
print("Successfully deleted!")
exc = input("\nEnter 'y' to delete more, 'n' to go back: ")
if exc != ('y' or 'Y'):
return
else:
continue
def write_key():
key = Fernet.generate_key()
with open("C:\\Users\\Public\\Documents\\Logged Database\\crypto.keylg", "wb") as key_file:
key_file.write(key)
def set_pass():
while 1:
pass1 = input("\nSet Password : ")
zeb = len(pass1)
if zeb < 6:
os.system('cls')
print("\n Password must at least 6 characters, please try again!")
continue
pass2 = input("\nConfirm Password: ")
if (pass1 == pass2):
try:
key = load_key()
decrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\pass.datlg", key)
except:
pass
with open("C:\\Users\\Public\\Documents\\Logged Database\\pass.datlg", "wb") as f:
pickle.dump(pass1, f)
os.system('cls')
print("\nNew password created successfully!")
f.close()
try:
key = load_key()
encrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\pass.datlg", key)
except:
pass
break
else:
os.system('cls')
print("\n'Set password' and 'Confirm password' must match, please try again!")
continue
return
def load_key():
return open("C:\\Users\\Public\\Documents\\Logged Database\\crypto.keylg", "rb").read()
def encrypt(filename, key):
f = Fernet(key)
with open(filename, "rb") as file:
file_data = file.read()
encrypted_data = f.encrypt(file_data)
with open(filename, "wb") as file:
file.write(encrypted_data)
def decrypt(filename, key):
f = Fernet(key)
with open(filename, "rb") as file:
encrypted_data = file.read()
decrypted_data = f.decrypt(encrypted_data)
with open(filename, "wb") as file:
file.write(decrypted_data)
def getdate():
return time.asctime(time.localtime(time.time()))
def addmem(ch_t, stri):
os.system('cls')
if not ch_t:
print("\t\t\t\t\tWelcome to Logged!\n")
print("\t\t\t\t\tAuthor: ady\n")
if stri == "user":
print("User database empty!\n")
print("Add a user to continue!\n")
else:
print("Log database empty!\n")
print("Add a log type to continue!\n")
if ch_t:
os.system('cls')
print("Adding new", stri, "!\n")
while True:
count = 1
con = 1
for ditckeys in ch_t:
count += 1
if ch_t:
print("Enter new", stri, "or '0' to return to Main Menu ")
else:
print("Enter new", stri, "or '0' to exit ")
mem = input("--> ")
try:
con = int(mem)
except:
pass
if con == 0:
if ch_t:
return
else:
sys.exit("EXIT")
if stri == "user":
mem = mem.capitalize()
for values in ch_t.values():
if mem == values:
os.system('cls')
print("There is already a", stri,
"named", mem, ",please try again!")
continue
else:
pass
if stri == "user":
users[count] = mem
else:
logs[count] = mem
print("\n", stri, " added successfully!\n")
if stri == "user":
try:
key = load_key()
decrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\users.datlg.datlg", key)
except:
pass
with open("C:\\Users\\Public\\Documents\\Logged Database\\users.datlg", "wb") as f:
pickle.dump(users, f)
key = load_key()
encrypt("C:\\Users\\Public\\Documents\\Logged Database\\users.datlg", key)
else:
try:
key = load_key()
decrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg", key)
except:
pass
with open("C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg", "wb") as f:
pickle.dump(logs, f)
key = load_key()
encrypt("C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg", key)
choice = input("Enter 'y' to add more, 'n' to return to Main Menu: ")
if choice != ('y' or 'Y'):
os.system('cls')
return
else:
os.system('cls')
continue
def delmem(ch_t, stri):
while True:
cnt = 0
os.system('cls')
print("Removing", stri, "!\n")
global users
global logs
for dickey, value in ch_t.items():
cnt = cnt+1
print("Enter", dickey, "to remove", value)
print("Enter '0' to return to Main Menu")
try:
mem = int(input("--> "))
except ValueError:
os.system('cls')
print("Invalid input, please try again!\n")
continue
if mem == 0:
os.system('cls')
return
if mem not in ch_t:
os.system('cls')
print("Wrong choice, please try again!\n")
continue
print("\n", stri, ch_t[mem], "removed successfully!\n")
if stri == "user":
for dickey2 in logs:
if os.path.exists("C:\\Users\\Public\\Documents\\Logged Database\\Logged" + str(mem) + "dot" + str(dickey2) + ".lg"):
os.remove("C:\\Users\\Public\\Documents\\Logged Database\\Logged" +
str(mem) + "dot" + str(dickey2) + ".lg")
else:
for dictkey in users:
if os.path.exists("C:\\Users\\Public\\Documents\\Logged Database\\Logged"+str(dictkey)+"dot"+str(mem)+".lg"):
os.remove("C:\\Users\\Public\\Documents\\Logged Database\\Logged" +
str(dictkey)+"dot"+str(mem)+".lg")
if stri == "user":
del users[mem]
if users:
temp = {}
count = 0
for items in users:
count += 1
temp[count] = users[items]
users = temp
ch_t = users
try:
key = load_key()
decrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\users.datlg", key)
except:
pass
with open("C:\\Users\\Public\\Documents\\Logged Database\\users.datlg", "wb") as f:
pickle.dump(users, f)
key = load_key()
encrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\users.datlg", key)
else:
if os.path.exists("C:\\Users\\Public\\Documents\\Logged Database\\users.datlg"):
os.remove(
"C:\\Users\\Public\\Documents\\Logged Database\\users.datlg")
return
else:
del logs[mem]
if logs:
temp = {}
count = 0
for items in logs:
count += 1
temp[count] = logs[items]
logs = temp
ch_t = logs
try:
key = load_key()
decrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg", key)
except:
pass
with open("C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg", "wb") as f:
pickle.dump(users, f)
key = load_key()
encrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg", key)
else:
if os.path.exists("C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg"):
os.remove(
"C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg")
return
if ch_t:
choice = input(
"Enter 'y' to remove more, 'n' to return to Main Menu: ")
if choice != ('y' or 'Y'):
os.system('cls')
return
else:
os.system('cls')
continue
else:
return
def log():
while True:
print("Logging data!\n")
for dickey, value in users.items():
print("Enter", dickey, "for", value)
print("Enter '0' to return to Main Menu")
try:
mem_key = int(input("--> "))
except ValueError:
os.system('cls')
print("Invalid input, please try again!\n")
continue
if mem_key == 0:
os.system('cls')
return
elif mem_key not in users:
os.system('cls')
print("Wrong choice, please try again!\n")
continue
while True:
os.system('cls')
print("Selected user:", users[mem_key], "\n")
for dickey, value in logs.items():
print("Enter", dickey, "to log", value)
print("Enter '0' to go back")
try:
log_key = int(input("--> "))
except ValueError:
os.system('cls')
print("Invalid input, please try again!\n")
continue
if log_key == 0:
os.system('cls')
break
elif log_key not in logs:
os.system('cls')
print("Wrong choice, please try again!\n")
continue
else:
while (True):
os.system('cls')
print("Enter", logs[log_key], "for", users[mem_key])
text = input("--> ")
text = text.capitalize()
try:
key = load_key()
decrypt("C:\\Users\\Public\\Documents\\Logged Database\\Logged" +
str(mem_key)+"dot"+str(log_key)+".lg", key)
except:
pass
if os.path.exists("C:\\Users\\Public\\Documents\\Logged Database\\Logged"+str(mem_key)+"dot"+str(log_key)+".lg"):
pass
else:
with open("C:\\Users\\Public\\Documents\\Logged Database\\Logged"+str(mem_key)+"dot"+str(log_key)+".lg", "wb") as f:
pickle.dump("\n", f)
with open("C:\\Users\\Public\\Documents\\Logged Database\\Logged"+str(mem_key)+"dot"+str(log_key)+".lg", "ab") as f:
pickle.dump(str(getdate())+" "+text+"\n", f)
key = load_key()
encrypt("C:\\Users\\Public\\Documents\\Logged Database\\Logged" +
str(mem_key)+"dot"+str(log_key)+".lg", key)
print("\n", users[mem_key], "'s",
logs[log_key], "recorded!\n")
if len(logs) > 1:
choice = input(
"Enter 'y' to log more "+logs[log_key]+" for "+users[mem_key]+", 'n' to go back, '0' for Main Menu: ")
if choice == '0':
return
if choice != ('y' or 'Y'):
break
else:
continue
else:
choice = input("Enter 'y' to log more " + logs[log_key] + " for " + users[
mem_key] + ", '0' for Main Menu: ")
if choice != ('y' or 'Y'):
os.system('cls')
return
else:
continue
os.system('cls')
if len(users) > 1:
choice = input("Enter 'y' to log more data"+" for " +
users[mem_key] + ", 'n' to log data for other users, '0' to return to Main Menu: ")
if choice == '0':
return
if choice != ('y' or 'Y'):
os.system('cls')
break
else:
os.system('cls')
continue
else:
continue
continue
def retrieve():
while True:
print("Retrieving Data!\n")
for key, value in users.items():
print("Enter", key, "for", value)
print("Enter '0' to return to Main Menu")
try:
mem_key = int(input("--> "))
except ValueError:
os.system('cls')
print("Invalid input, please try again!\n")
continue
if mem_key == 0:
os.system('cls')
return
elif mem_key not in users:
os.system('cls')
print("Wrong choice, please try again!\n")
continue
os.system('cls')
while True:
print("Selected user:", users[mem_key], "\n")
for dickey, value in logs.items():
print("Enter", dickey, "to retrieve", value)
print("Enter 'n' to go back, '0' to return to Main Menu")
try:
log_key = int(input("--> "))
except ValueError:
os.system('cls')
break
if log_key == 0:
os.system('cls')
return
elif log_key not in logs:
os.system('cls')
print("Wrong choice, please try again!\n")
continue
if os.path.exists("C:\\Users\\Public\\Documents\\Logged Database\\Logged"+str(mem_key)+"dot"+str(log_key)+".lg"):
ct = display(mem_key, log_key)
if (len(logs) > 1 or len(users) > 1):
print("Enter:\n 'd' to delete specific logs\n 'e' to delete entire " + logs[
log_key] + " log for " + users[
mem_key] + ",\n 'n' to go back,\n '0' to return to Main Menu")
else:
print("Enter:\n 'd' to delete specific logs\n 'e' to delete entire " + logs[
log_key] + " log for " + users[
mem_key] + "\n '0' to return to Main Menu")
choice = input("--> ")
if choice == '0':
os.system('cls')
return
elif choice == ('d' or 'D'):
dellog(mem_key, log_key)
break
elif choice == ('e' or 'E'):
os.system('cls')
exc = input("Enter 'y' to confirm deletion of " +
users[mem_key]+"'s"+" entire log of "+logs[log_key]+", or 'n' to go back: ")
if exc != ('y' or 'Y'):
os.system('cls')
continue
elif os.path.exists("C:\\Users\\Public\\Documents\\Logged Database\\Logged"+str(mem_key)+"dot"+str(log_key)+".lg"):
os.remove("C:\\Users\\Public\\Documents\\Logged Database\\Logged" +
str(mem_key)+"dot"+str(log_key)+".lg")
os.system('cls')
print(users[mem_key]+"'s"+" entire log of " +
logs[log_key]+" successfully deleted!\n")
break
elif choice == ('n' or 'N'):
os.system('cls')
continue
else:
os.system('cls')
print("There are no logs for",
users[mem_key], "'s", logs[log_key], "!\n")
break
continue
def wel():
global pp
key = load_key()
decrypt("C:\\Users\\Public\\Documents\\Logged Database\\pass.datlg", key)
with open("C:\\Users\\Public\\Documents\\Logged Database\\pass.datlg", "rb") as f:
pp = pickle.load(f)
key = load_key()
encrypt("C:\\Users\\Public\\Documents\\Logged Database\\pass.datlg", key)
while (1):
p = input("Password: ")
if p == pp:
os.system('cls')
break
else:
while 1:
os.system('cls')
print("Incorrect password!\n")
print("Enter:\n '1' to try again\n '0' to exit\n 'r' to reset data ")
i = input("-->")
if i == '1':
os.system('cls')
break
elif i == '0':
sys.exit("EXIT")
elif i == 'r':
os.system('cls')
print("Enter 'y' to confirm database reset, 'n' to go back")
df2 = input("--> ")
if df2 == ('y' or 'Y'):
dres()
os.system('cls')
print("Database reset successful!\n")
return 16
else:
os.system('cls')
break
return 0
def main():
global users, logs
while (1):
key = load_key()
if os.path.exists("C:\\Users\\Public\\Documents\\Logged Database\\users.datlg"):
try:
decrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\users.datlg", key)
try:
with open("C:\\Users\\Public\\Documents\\Logged Database\\users.datlg", "rb") as f:
while 1:
try:
users = pickle.load(f)
except EOFError:
break
except:
pass
except:
pass
try:
encrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\users.datlg", key)
except:
pass
if os.path.exists("C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg"):
try:
decrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg", key)
try:
with open("C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg", "rb") as f:
while 1:
try:
logs = pickle.load(f)
except EOFError:
break
except:
pass
except:
pass
try:
encrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\logs.datlg", key)
except:
pass
if not users:
addmem(users, "user")
if not logs:
addmem(logs, "log type")
print("\t\t\t\t\tLogged in Logged!\n")
print("\t\t\t\t\tAuthor: ady\n")
print(
"Main Menu\n\nEnter:\n '1' to log data\n '2' to retrieve/delete data\n '3' to add users\n '4' to add log types\n"
" '5' remove users/log types/reset database\n '6' to change password\n '0' to exit")
try:
z = int(input("--> "))
except ValueError:
os.system('cls')
print("Invalid input, please try again!\n")
continue
if z == 1:
os.system('cls')
log()
elif z == 2:
os.system('cls')
retrieve()
elif z == 3:
os.system('cls')
addmem(users, "user")
elif z == 4:
os.system('cls')
addmem(logs, "log type")
elif z == 5:
os.system('cls')
#delmem(users, "user")
while 1:
print(
"Enter:\n '1' to remove user\n '2' to remove log type\n '3' to reset database\n '0' to return to Main Menu")
try:
ef = int(input())
pass
except:
os.system('cls')
print("Invalid input, please try again!\n")
continue
if ef == 0:
break
if ef == 1:
delmem(users, "user")
break
elif ef == 2:
delmem(logs, "log type")
break
elif ef == 3:
os.system('cls')
print("Enter 'y' to confirm database reset, 'n' to go back")
df = input("--> ")
if df == ('y' or 'Y'):
dres()
os.system('cls')
print("Database reset successful!\n")
start()
break
else:
break
else:
os.system('cls')
print("Wrong choice, please try again!\n")
continue
elif z == 6:
os.system('cls')
while 1:
pp = ""
key = load_key()
decrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\pass.datlg", key)
with open("C:\\Users\\Public\\Documents\\Logged Database\\pass.datlg", "rb") as f:
pp = pickle.load(f)
key = load_key()
encrypt(
"C:\\Users\\Public\\Documents\\Logged Database\\pass.datlg", key)
print("Enter '0' to return to main menu")
pass3 = input("\nEnter current password: ")
if pass3 == 0:
break
if pass3 == pp:
os.system('cls')
set_pass()
break
else:
os.system('cls')
print("Incorrect password, try again!\n")
continue
elif z == 0:
os.system('cls')
sys.exit("EXIT")
else:
os.system('cls')
print("Wrong choice, please try again!\n")
continue
def start():
while 1:
try:
os.mkdir("C:\\Users\\Public\\Documents\\Logged Database")
except FileExistsError:
pass
try:
key = load_key()
decrypt("C:\\Users\\Public\\Documents\\Logged Database\\pass.datlg", key)
key = load_key()
encrypt("C:\\Users\\Public\\Documents\\Logged Database\\pass.datlg", key)
t = wel()
if t == 16:
continue
else:
break
except:
dres()
os.mkdir("C:\\Users\\Public\\Documents\\Logged Database")
print("\t\t\t\t\tLogged in Logged!\n")
print("\t\t\t\t\tAuthor: ady\n")
print("\nHere's to new beginnings!")
print("\nLet's start by setting up a password for database!")
write_key()
set_pass()
break
users = {}
logs = {}
start()
main()