-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_04
executable file
·40 lines (28 loc) · 1.01 KB
/
exercise_04
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
#!/usr/bin/env python3
# Copyright 2014 Carl Winbäck. See the COPYING file at the top-level directory
# of this distribution.
from operator import itemgetter
from libpals.util import xor_find_singlechar_key
def get_winner(input_file):
indata = [line.strip() for line in open(input_file)]
candidates = list()
for line in indata:
ciphertext = bytes.fromhex(line)
result = xor_find_singlechar_key(ciphertext)
result['ciphertext_hex'] = line
candidates.append(result)
winner = max(candidates, key=itemgetter('score'))
return winner
winner = get_winner('data/4.txt')
output_message = """SET 01 CHALLENGE 04: Detect single-character XOR
Key:
Decimal: {}
Hex: {}
ASCII: {}
Hex: {}
Message: {}"""
print(output_message.format(winner['key'],
hex(winner['key']),
chr(winner['key']),
winner['ciphertext_hex'],
str(object=winner['plaintext'], encoding='ascii')))