forked from gcjensen/xor-cipher-cracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·27 lines (25 loc) · 1.1 KB
/
example.py
File metadata and controls
executable file
·27 lines (25 loc) · 1.1 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
import sys
import binascii
from xor_cipher_cracker import XORCipherCracker
def convert_hex_file_to_binary_string(hex_file):
hex_string = binascii.hexlify(hex_file).replace('\n', '')
binary = [
'0000','0001','0010','0011',
'0100','0101','0110','0111',
'1000','1001','1010','1011',
'1100','1101','1110','1111'
]
binary_string = ''
for i in range(len(hex_string)):
binary_string += binary[int(hex_string[i], base=16)]
return binary_string
if __name__ == '__main__':
with open(sys.argv[1], 'r') as hex_file:
cipher_text_in_binary = convert_hex_file_to_binary_string(hex_file.read())
# the list of characters we believe the plaintext to contain
plain_text_chars = 'abcdefghijklmnopqrstuvwxyz .,-\'?_()!'
xor_cipher_cracker = XORCipherCracker(plain_text_chars)
possible_decryptions = xor_cipher_cracker.crack(cipher_text_in_binary)
for key in possible_decryptions.keys():
print('Decryption with key: ' + hex(int(key, 2)))
print(binascii.unhexlify('%x' % possible_decryptions[key]))