cryptopals/04.py

28 lines
911 B
Python

import helper, cryptlib
def bruteforce_xor(hexstring_list):
plaintext_candidates = []
for i in range(len(hexstring_list)):
hexstring = hexstring_list[i]
for key in range(256):
decrypt = cryptlib.single_byte_xor(hexstring, key)
english_score = cryptlib.score_english_lang(decrypt)
result = {
'line': i+1,
'key': key,
'score': english_score,
'plaintext': decrypt.strip(),
'cipertext': hexstring
}
plaintext_candidates.append(result)
return plaintext_candidates
def main():
content = helper.read_file("data/04.txt")
hexstring_list = content.splitlines()
plaintext_candidates = bruteforce_xor(hexstring_list)
print(sorted(plaintext_candidates, key=lambda c: c['score'], reverse=True)[0])
if __name__ == '__main__':
main()