Guest User

Untitled

a guest
May 26th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. from itertools import cycle
  2.  
  3. cipher = [int(i) for i in input('Enter ciphertext:\n').split()]
  4.  
  5. splits = [cipher[::3], cipher[1::3], cipher[2::3]]
  6.  
  7.  
  8. def distance_mod_256(x, y):
  9. return min((x - y) % 256, (y - x) % 256)
  10.  
  11.  
  12. def find_whitespace(iterable):
  13. # whitespace is the 'outlier' in the cyclic group
  14. # the sum of it's distances from the other characters is the largest
  15. sum_distances = lambda x: sum([distance_mod_256(x, i) for i in iterable])
  16. return max(iterable, key=sum_distances)
  17.  
  18.  
  19. offsets = [find_whitespace(split) - 32 for split in splits] # whitespace is 32 in ascii
  20.  
  21. decrypted = []
  22. for split, offset in zip(splits, offsets):
  23. decrypted.append([chr((i-offset) % 256) for i in split])
  24.  
  25. iters = [iter(text) for text in decrypted]
  26. print(''.join(next(it) for it in cycle(iters)))
Add Comment
Please, Sign In to add comment