-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathS1C04.py
26 lines (18 loc) · 965 Bytes
/
S1C04.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
from S1C03 import singlechar_xor_brute_force, pretty_print_result
def detect_encrypted_text(encrypted_strings):
"""Performs a singlechar XOR brute force attack to every ciphertext of the input, gets a plaintext
from each of the ciphertexts and returns the decrypted plaintext which has the highest English score.
"""
candidates = []
for string in encrypted_strings:
candidates.append(singlechar_xor_brute_force(string))
# Return the candidate with the highest English score
return sorted(candidates, key=lambda c: c['score'], reverse=True)[0]
def main():
ciphertexts = [bytes.fromhex(line.strip()) for line in open("S1C04_input.txt")]
most_likely_plaintext = detect_encrypted_text(ciphertexts)
pretty_print_result(most_likely_plaintext)
# Check that the attack works properly
assert most_likely_plaintext['plaintext'].rstrip() == b"Now that the party is jumping"
if __name__ == "__main__":
main()