-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryptor.py
101 lines (78 loc) · 2.92 KB
/
encryptor.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
import time
from AES import encrypt, decrypt
import argparse
class FilesEncryptor:
def __init__(self, name, output_name='enc'):
self.key = None
self.name = name
self.output = output_name
def set_output(self, output):
self.output = output
def set_key(self, key):
self.key = key
def generateFile(self):
with open(self.name, 'rb') as f:
f = bytes.hex(f.read())
self.name = encrypt(self.name.encode('utf-8').hex(), self.key)
enc = encrypt(f, self.key)
with open(self.output, 'wb') as f:
f.write(bytes.fromhex(self.name) + b'\n')
f.write(bytes.fromhex(enc))
class FilesDecryptor:
def __init__(self, name, output_name=None):
self.key = None
self.name = name
self.output = output_name
def set_key(self, key):
self.key = key
def set_output(self, output):
self.output = output
def generateFile(self):
with open(self.name, 'rb') as f:
self.name = bytes.hex(f.readline())[:-2]
f = bytes.hex(f.read())
if self.output is None:
self.output = self.removeNullCharacters(decrypt(self.name, self.key))
self.output = bytes.fromhex(self.output).decode('utf-8')
self.output = self.output.split('.')
self.output[0] = self.output[0] + '(1)'
self.output = '.'.join(self.output)
data = self.removeNullCharacters(decrypt(f, self.key))
with open(self.output, 'wb') as f:
f.write(bytes.fromhex(data))
def removeNullCharacters(self, data):
while data[-2:] == '00':
data = data[:-2]
return data
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--encrypt", help="encrypt given file")
parser.add_argument("-d", "--decrypt", help="decrypt given file")
parser.add_argument("-k", "--key", required=True, help="key for encryption/decryption")
parser.add_argument("-o", "--output", help="name of output file")
args = parser.parse_args()
passed_args = True
if args.encrypt:
file_manager = FilesEncryptor(args.encrypt)
elif args.decrypt:
file_manager = FilesDecryptor(args.decrypt)
else:
passed_args = False
print("Please specify either -e or -d")
if passed_args:
if args.output is not None: file_manager.set_output(args.output)
key = args.key
try:
int(key, 16)
except ValueError:
print("Key is not valid. Key must be 32, 64 or 48 hexadecimal characters.")
valid = False
valid = True
if len(key) != 32 and len(key) != 64 and len(key) != 48:
print("Key is not valid")
valid = False
if valid:
file_manager.set_key(key)
x1 = time.time()
file_manager.generateFile()
x2 = time.time()
print(f"Finished successfully in {x2 - x1} seconds")