-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
153 lines (127 loc) · 5.73 KB
/
Copy pathmain.py
File metadata and controls
153 lines (127 loc) · 5.73 KB
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
from base64 import urlsafe_b64encode as b64encode
from itertools import count
from cryptography.fernet import Fernet
from os import remove, system, urandom, chdir, getcwd, listdir
from os.path import abspath, isdir
from json import load as json_load
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from time import sleep
class colors:
RESET = '\033[0m'
BLUE = '\033[94m'
RED = '\033[91m'
YELLOW = '\033[93m'
GREEN = '\u001b[32m'
CUR_CLR = '\033[90m'
ERROR = '\033[91m'
SUCCESS = '\u001b[38;5;82m'
WARNING = '\033[93m'
class Cipher:
def __init__(self):
system("cls||clear")
self.prev_dir = getcwd()
chdir( abspath(f"{__file__}/..") )
conf = json_load(open("config.json", "rb"))
print(f"{colors.GREEN + conf['AppArt'] + colors.CUR_CLR}\nver {conf['AppVersion'] + colors.RESET}\n")
while True:
print(f"{colors.YELLOW + getcwd() + colors.RESET}")
send_cmd = input(f"{colors.CUR_CLR}>> {colors.RESET}").split(" ")
if "exit" in send_cmd:
print(f"{colors.YELLOW}Exiting...{colors.RESET}")
sleep(2)
chdir(abspath(self.prev_dir))
break
print(self.cmd_parse(send_cmd))
def cmd_parse(self, cmd:list):
if "encrypt" in cmd:
key = self.load_key()
return self.encrypt(cmd[1], key)
elif "decrypt" in cmd:
key = self.load_key()
return self.decrypt(cmd[1], key)
elif "cd" in cmd:
chdir(abspath(cmd[1]))
return ""
elif "ls" in cmd:
wet_list = listdir()
dirlist = ""
for path in wet_list:
if( isdir(path) == 1 ):
dirlist = f"{ dirlist + colors.BLUE + path + colors.RESET }\n"
elif( isdir(path) == 0 and ".encrypted" in path ):
dirlist = f"{ dirlist + colors.YELLOW + path + colors.RESET }\n"
elif( isdir(path) == 0 and ".encrypted" not in path ):
dirlist = f"{ dirlist + path }\n"
return f"{dirlist}"
elif "keygen" in cmd:
password = input(f"Key file password:{colors.YELLOW} ")
return self.write_key(password)
else:
return f"{colors.ERROR}[!]{colors.RESET} Command not found.\n"
def write_key(self, password:str):
salt = urandom(16)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=390000,
)
pass_bytes = password.encode("utf-8")
key = b64encode(kdf.derive(pass_bytes))
open(abspath(f"{__file__}/../crypto.key"), "wb").write(key)
return f"{colors.RESET}Key file generated."
def load_key(self):
return open(abspath(f"{__file__}/../crypto.key"), "rb").read()
def encrypt(self, filename:str, key:bytes):
fernet = Fernet(key)
if "*" in filename:
wet_file_list = listdir()
file_list = list()
for file in wet_file_list:
if isdir(file) == 0:
file_list.append(file)
for file in file_list:
with open(file, "rb") as f:
file_data = f.read()
encrypted_data = fernet.encrypt(file_data)
open(f"{file}.encrypted", "wb").write(encrypted_data)
remove(file)
return f"{colors.SUCCESS}[+]{colors.RESET} {colors.YELLOW + str(len(file_list)) + colors.RESET} files were successfully encrypted.\n"
else:
with open(filename, "rb") as f:
file_data = f.read()
encrypted_data = fernet.encrypt(file_data)
open(f"{filename}.encrypted", "wb").write(encrypted_data)
remove(filename)
return f"{colors.SUCCESS}[+]{colors.RESET} File '{colors.YELLOW + filename + colors.RESET}' encrypted successfully.\n"
def decrypt(self, filename:str, key:bytes):
fernet = Fernet(key)
if "*" in filename:
wet_file_list = listdir()
file_list = list()
for file in wet_file_list:
if isdir(file) == 0 and ".encrypted" in file:
file_list.append(file)
for file in file_list:
with open(file, "rb") as f:
encrypted_data = f.read()
data = fernet.decrypt(encrypted_data)
open(file[:-10], "wb").write(data)
remove(file)
return f"{colors.SUCCESS}[+]{colors.RESET} {colors.YELLOW + str(len(file_list)) + colors.RESET} files were successfully decrypted.\n"
else:
if ".encrypted" not in filename:
encrypted_data = open(f"{filename}.encrypted", "rb").read()
data = fernet.decrypt(encrypted_data)
open(filename, "wb").write(data)
remove(f"{filename}.encrypted")
return f"{colors.SUCCESS}[+]{colors.RESET} File '{colors.YELLOW + f'{filename}.encrypted' + colors.RESET}' decrypted successfully.\n"
elif ".encrypted" in filename:
encrypted_data = open(filename, "rb").read()
data = fernet.decrypt(encrypted_data)
open(filename[:-10], "wb").write(data)
remove(filename)
return f"{colors.SUCCESS}[+]{colors.RESET} File '{colors.YELLOW + filename + colors.RESET}' decrypted successfully.\n"
if __name__ == "__main__":
Cipher()