-
Notifications
You must be signed in to change notification settings - Fork 76
/
xor_encryptor.py
executable file
·66 lines (52 loc) · 1.92 KB
/
xor_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
#!/usr/bin/env python3
import sys
KEY = "CHANGEME"
class XorCipher:
__slots__ = ("key", "_key_length", "_fname", "_ciphertext", "_plaintext")
def __init__(self, filename: str, xor_key: str) -> None:
self.key = str(xor_key)
self._key_length = len(self.key)
self._fname = filename
self._ciphertext = ""
self._plaintext = b""
def _xor_crypt(self) -> None:
i = 0
for char in self._plaintext:
self._ciphertext += chr(char ^ ord(self.key[i % self._key_length]))
i += 1
def _print_ciphertext(self) -> None:
from textwrap import TextWrapper
wrapper = TextWrapper(width=56, initial_indent="\n")
xor_array = ("{ 0x" +
", 0x".join(hex(ord(x))[2:].zfill(2).upper()
for x in self._ciphertext) + " };")
wrapped_xor_array = wrapper.fill(xor_array)
print(wrapped_xor_array)
def run(self) -> None:
try:
with open(self._fname, "rb") as fp:
self._plaintext = fp.read()
except Exception as e:
print(f"[-] Error with specified file({self._fname}): {e}",
file=sys.stderr)
sys.exit(1)
else:
self._xor_crypt()
self._print_ciphertext()
return
def main():
# xor key should be similar to the one in the C++ file(fud-uuid-shc.cpp). Please
# endeavour to change it!!
# Also the "file" opened by default is the file you supply at the command line
# NOTE: You can port this class( XorCipher ) to your own scripts neatly.
try:
xor_crypt = XorCipher(filename=sys.argv[1], xor_key=KEY)
except IndexError:
print("[-] File argument needed! \n\t%s <file_to_xor_encrypt>" %
sys.argv[0],
file=sys.stderr)
sys.exit(1)
else:
xor_crypt.run()
if __name__ == "__main__":
main()