Hackvent_2018/Teaser/_ZOoxjUSe1OVB7OPoVrsX.pdf.extracted/break_santa.py
Roman Hergenreder 8b34218479 Day 14?
2018-12-14 17:11:03 +01:00

85 lines
2.7 KiB
Python

#!/usr/bin/python
from itertools import chain, product, permutations
# Santa has caught up with the information age and does not trust
# clear-text commands anymore.
# He has decided that all communications
# have to be encrypted to prevent an unfriendly take-over of his team.
# Santa chooses a simple, secure, and toolless encryption scheme.
# However, his team's memory capacity is limited and so he can only use
# their names (Dasher, Dancer, Prancer, Vixen, Comet, Cupid, Donder and
# Blitzen) as keys.
encrypted = "STTYN YATLOEP DNEA ONBL TGNTO MHEHH EISTIARIB FHSRA LD IIONA NL HERUV LN17-PTAA-RTON-RDOE-MCTN-AHCO"
encrypted_words = encrypted.split(" ")
def load_words():
with open('words_alpha.txt') as word_file:
valid_words = set(word_file.read().split())
return valid_words
english_words = load_words()
# totalCount = 0
# letter_count = {}
# for word in encrypted_words[:-1]:
# totalCount += len(word)
# for letter in word:
# if not letter in letter_count:
# letter_count[letter] = 1
# else:
# letter_count[letter] += 1
# print(letter_count, totalCount)
# def bruteforce(charset, minlength, maxlength):
# return (''.join(candidate)
# for candidate in chain.from_iterable(product(charset, repeat=i)
# for i in range(minlength, maxlength + 1)))
#
# def generateKey(cipher, plain):
# key = ""
# for i in range(min(len(cipher), len(plain))):
# key += chr(ord('A') + ord(cipher[i]) - ord(plain[i]))
# return key
#
def decrypt(cipher, key):
while len(key) < len(cipher):
key += key
return ''.join([chr(ord('A') + (ord(cipher[i]) + ord(key[i]) - 2 * ord('A')) % 26) for i in range(len(cipher))])
possible_keys = ["DASHER","DANCER","PRANCER","VIXEN","COMET","CUPID","DONDER","BLITZEN"]
# startingLetters = [key[0] for key in possible_keys]
# processedKeys = []
#
# for p in permutations(startingLetters, len(startingLetters)):
# key = ''.join(p)
# if key not in processedKeys:
# processedKeys.append(key)
# plain = decrypt(encrypted_words[0], key)
# if plain.lower() in english_words:
# print(key, plain)
for key in possible_keys:
print(key, decrypt(encrypted_words[0], key[::-1]))
#
# for key in bruteforce("ABCDEFGHIJKLMNOPQRSTUVXYZ", 5, 5):
# plain = decrypt(encrypted_words[0], key)
# if key.lower() in english_words and plain.lower() in english_words:
# print(key, plain)
# for word in english_words:
# if len(word) == 5:
# word = word.upper()
# key = generateKey(encrypted_words[0], word).lower()
# # if key.isalpha():
# if key in english_words:
# print(word, key)