Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import cycle
- cipher = [int(i) for i in input('Enter ciphertext:\n').split()]
- splits = [cipher[::3], cipher[1::3], cipher[2::3]]
- def distance_mod_256(x, y):
- return min((x - y) % 256, (y - x) % 256)
- def find_whitespace(iterable):
- # whitespace is the 'outlier' in the cyclic group
- # the sum of it's distances from the other characters is the largest
- sum_distances = lambda x: sum([distance_mod_256(x, i) for i in iterable])
- return max(iterable, key=sum_distances)
- offsets = [find_whitespace(split) - 32 for split in splits] # whitespace is 32 in ascii
- decrypted = []
- for split, offset in zip(splits, offsets):
- decrypted.append([chr((i-offset) % 256) for i in split])
- iters = [iter(text) for text in decrypted]
- print(''.join(next(it) for it in cycle(iters)))
Add Comment
Please, Sign In to add comment