-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr0t3ct.py
162 lines (137 loc) · 4.16 KB
/
pr0t3ct.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
from typing import Text
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import base64
import hashlib
import getpass
import os
from art import *
salt = b'K\x8d\xb9\x86\xf7\x11\\\x14\xe8\x84\x16l\x8d+X\xe3'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
)
def clearscr():
if os.name == 'nt':
os.system("cls")
else:
os.system("clear")
def secure_del(file):
try:
delfile = open(file,'wb')
delfile.write(os.urandom(delfile.tell()))
delfile.close()
os.unlink(file)
except Exception as err:
print(err)
def keygen(password):
return base64.urlsafe_b64encode(kdf.derive(password))
def encrypt(key,path):
fernet = Fernet(key)
file = open(path,'rb')
filedata = file.read()
file.close()
encrypted = fernet.encrypt(filedata)
output_file = path.replace(os.path.basename(path),os.path.basename(path)+'.pr0t3ct')
efile = open(output_file,'wb')
efile.write(encrypted)
efile.close()
print(f"File encrypted sucessfully as {output_file}")
def decrypt(key,path):
fernet = Fernet(key)
file = open(path,'rb')
filedata = file.read()
file.close()
decrypted = fernet.decrypt(filedata)
output_file = path.replace(os.path.basename(path),os.path.basename(path).strip('.pr0t3ct'))
efile = open(output_file,'wb')
efile.write(decrypted)
efile.close()
print(f"File decrypted sucessfully as {output_file} ")
def file_handler(file_path,key):
output_name = os.path.basename(file_path)
if os.path.exists(file_path) and not file_path.endswith('.pr0t3ct'):
encrypt(key,file_path)
elif os.path.exists(file_path) and file_path.endswith('.pr0t3ct'):
decrypt(key,file_path)
else:
print('File does not exist.')
quit()
def hashpass(password):
return hashlib.sha512(password.encode()).hexdigest()
def signup():
username = input('Enter username: ')
password = getpass.getpass(prompt="Enter Password: ")
hashedpass = hashpass(password)
try:
os.mkdir('dep/'+username)
pfile = open('dep/'+username+'/'+'shadow','w')
pfile.write(hashedpass)
print('Profile created sucessfully')
except FileExistsError:
print('user already exists')
def signin():
username = input('Enter username: ')
password = getpass.getpass(prompt="Enter Password: ")
hashedpass = hashpass(password)
key = keygen(password.encode())
if os.path.exists('dep/'+username):
hashfile = open('dep/'+username+'/shadow','rb')
stored_hash = hashfile.read()
hashfile.close()
if str(hashedpass) != str(stored_hash.decode()):
print('access denied')
quit()
else:
menu(key=key,username=username)
else:
print('user does not exist\nSignup first')
quit()
def menu(key,username):
clearscr()
print('*'*50)
print('*'*50)
print(f'Welcome :-: [{username}]')
print('*'*50)
print('*'*50)
print('[1]-[Encrypt File]')
print('[2]-[Decrypt File]')
print('[3]-[Exit]')
option = input('Selcet your option: ')
clearscr()
if option == '1':
filepath = input('Enter path of file: ')
op = input("Do you want to delete the orignal file? (y/n): ")
if op.lower() == 'y':
file_handler(filepath,key)
secure_del(filepath)
else:
file_handler(filepath,key)
elif option == '2':
filepath = input('Enter path of file: ')
op = input("Do you want to delete the encrypted file? (y/n): ")
if op.lower() == 'y':
file_handler(filepath,key)
secure_del(filepath)
else:
file_handler(filepath,key)
elif option == '3':
quit()
else:
print('Wrong option')
quit()
tprint("Pr0t3ct","random")
print('1:Signup\n2:Signin\n3:exit')
option = input('Choose your option: ')
if option == '1':
signup()
elif option == '2':
signin()
elif option == '3':
quit()
else:
print('Wrong option')
quit()