-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXoR_Locker.py
More file actions
190 lines (149 loc) · 9.54 KB
/
XoR_Locker.py
File metadata and controls
190 lines (149 loc) · 9.54 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
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
"""
#=========================================================#
# [+] Title: Image XoR Encryption and Decryption #
# [+] Script: XoR_Locker.py #
# [+] Creator : Danyah Alharthi #
#=========================================================#
"""
import argparse
import sys
import hashlib
from random import randint
class bcolors:
FAIL = '\033[91m'
BANNER = '\033[93m'
HELP = '\033[42m'
ENDC = '\033[0m'
INPUT = '\033[94m'
DONE = '\033[92m'
class xorLocker:
def __init__(self):
self.path = ''
@staticmethod
def get_args():
parser = argparse.ArgumentParser(description=print(bcolors.BANNER + '''
██╗░░██╗░█████╗░██████╗░ ██╗░░░░░░█████╗░░█████╗░██╗░░██╗███████╗██████╗░
╚██╗██╔╝██╔══██╗██╔══██╗ ██║░░░░░██╔══██╗██╔══██╗██║░██╔╝██╔════╝██╔══██╗
░╚███╔╝░██║░░██║██████╔╝ ██║░░░░░██║░░██║██║░░╚═╝█████═╝░█████╗░░██████╔╝
░██╔██╗░██║░░██║██╔══██╗ ██║░░░░░██║░░██║██║░░██╗██╔═██╗░██╔══╝░░██╔══██╗
██╔╝╚██╗╚█████╔╝██║░░██║ ███████╗╚█████╔╝╚█████╔╝██║░╚██╗███████╗██║░░██║
╚═╝░░╚═╝░╚════╝░╚═╝░░╚═╝ ╚══════╝░╚════╝░░╚════╝░╚═╝░░╚═╝╚══════╝╚═╝░░╚═╝
Tool :- XOR Locker
Tool type :- Image Encryption and Decryption using XOR Operartion
Created by :- Danyah Alharthi
Syntax : python <filename> <option> <path>
__________________________________________________________________________________________
| |
| -e or --encryption { |
| Use : To Encrypt an Image File |
| Explaination : The Path of the Image you want to Encrypt |
| } |
| |
| -d or --decryption { |
| Use : To Decrypt an Image File |
| Explaination : The Path of the Image you want to Decrypt |
| |
| } |
| |
| -c or --compare { |
| Use : To Compare Between Two Hashes |
| Explaination : The Path of the Image you want to Compare its Hash |
| |
| } |
| |
| -cm or --computeHash { |
| Use : To Compute the Hash of an Image |
| Explaination : The Path of the Image you want to Compare its Hash |
| } |
| Example { |
| Command : python XoR_Locker.py -e /Desktop/example_dir/example_img |
| } |
\_________________________________________________________________________________________/
''' + bcolors.ENDC))
parser.add_argument("-op", "--options", help=bcolors.BANNER+"Listing all Options"+bcolors.ENDC)
parser.add_argument("-e", "--encryption", help=bcolors.BANNER + "Encrypting the Image" + bcolors.ENDC)
parser.add_argument("-d", "--decryption", help=bcolors.BANNER + "Decrypting the Image" + bcolors.ENDC)
parser.add_argument("-c","--compare", help=bcolors.BANNER +"Compare the Hashes"+ bcolors.ENDC)
parser.add_argument('-cm', '--computeHash', help=bcolors.BANNER +"Calculate the Hash"+ bcolors.ENDC)
return parser.parse_args()
def imgEncrypter(self, path):
try:
# Open & read file from given path
with open(path, "rb") as input_file:
image = input_file.read()
image = bytearray(image)
key = randint(0, 100)
# Perform Encryption operation
for index, value in enumerate(image):
image[index] = value ^ key
# Write updated values in file from given path
with open(path, "wb") as output_file:
output_file.write(image)
# Storing Encryption key
with open('/Users/xxx/Desktop/example_dir/Encryption_Info.txt', 'w') as server:
server.write(f"Image Path: {path} --> Key {key}")
print(bcolors.DONE + "[✔] Encryption Done Successfully " + bcolors.ENDC)
except Exception:
print(bcolors.FAIL + "[✖] Fail to Encrypt" + bcolors.ENDC)
def imgDecrypter(self, path):
while True: # Keep running untill the user enters the correct key
try:
print(bcolors.INPUT + "Note : Encryption and Decryption Key Must Be The Same" + bcolors.INPUT)
key = int(input(bcolors.INPUT + "[+] Please Enter Image Key To Decrypt : " + bcolors.INPUT))
# Validating the entered key
with open('/Users/xxx/Desktop/example_dir/Encryption_Info.txt', 'r') as server:
line = server.readline()
line = line[-2:]
if key == int(line):
# Open & read file from given path
with open(path, "rb") as input_file:
image = input_file.read()
image = bytearray(image)
# Perform Decryption operation
for index, value in enumerate(image):
image[index] = value ^ key
# Write updated values in file from given path
with open(path, "wb") as output_file:
output_file.write(image)
print(bcolors.DONE + "[✔] Decryption Done Successfully " + bcolors.ENDC)
# Cleaning the file that contains Encryption keys
with open('/Users/xxx/Desktop/example_dir/Encryption_Info.txt', 'w') as file_earasing:
file_earasing.truncate()
file_earasing.close()
break
else:
print(bcolors.FAIL + "[✖] Key Is Not Correct"+ bcolors.ENDC)
except Exception:
print(bcolors.FAIL + "[✖] Fail to Decrypt" + bcolors.ENDC)
def computeHash(self, path):
with open(path, "rb") as input_file:
image = input_file.read()
Hash = hashlib.md5(image).hexdigest()
print(bcolors.DONE +"[✔] MD5: " + Hash + bcolors.ENDC)
def compareHash(self, path):
with open(path, "rb") as input_file:
image = input_file.read()
new = hashlib.md5(image).hexdigest()
old = input(bcolors.INPUT+"[+] Old Hash : "+bcolors.ENDC)
if old == new:
print(bcolors.HELP +"[+] New Hash : "+new+ bcolors.ENDC)
print(bcolors.DONE +"[✔] Hashes Matched"+ bcolors.ENDC)
else:
print(bcolors.FAIL +"[✖] Hash MisMatch"+bcolors.ENDC)
def main(self):
args = self.get_args()
if args.encryption:
self.imgEncrypter(args.encryption)
elif args.decryption:
self.imgDecrypter(args.decryption)
elif args.computeHash:
self.computeHash(args.computeHash)
elif args.compare:
self.compareHash(args.compare)
else:
print(bcolors.FAIL + '[✖] Invalid Syntax' + bcolors.ENDC)
print(bcolors.HELP + 'Use --help or -h for options.' + bcolors.ENDC)
sys.exit(1)
if __name__ == '__main__':
obj = xorLocker()
obj.main()