here2share

# unshuffle_plus.py

Jun 20th, 2021 (edited)
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. # unshuffle_plus.py
  2.  
  3. # had thought there might be a way to further compress data through this method
  4. # ... which is not the case, so it's been retracted. It was a fun attempt though.
  5.  
  6. from cStringIO import StringIO
  7. from PIL import ImageTk, Image
  8. import base64, zlib
  9. import random
  10.  
  11. key_cycles = 10
  12.  
  13. msg = '''
  14. Hello World!
  15.  
  16. THE ZEN OF PYTHON
  17.  
  18. Beautiful is better than ugly.
  19. Explicit is better than implicit.
  20. Simple is better than complex.
  21. Complex is better than complicated.
  22. Flat is better than nested.
  23. Sparse is better than dense.
  24. Readability counts.
  25. Special cases aren't special enough to break the rules.
  26. Although practicality beats purity.
  27. Errors should never pass silently.
  28. Unless explicitly silenced.
  29. In the face of ambiguity, refuse the temptation to guess.
  30. There should be one-- and preferably only one --obvious way to do it.
  31. Although that way may not be obvious at first unless you're Dutch.
  32. Now is better than never.
  33. Although never is often better than *right* now.
  34. If the implementation is hard to explain, it's a bad idea.
  35. If the implementation is easy to explain, it may be a good idea.
  36. Namespaces are one honking great idea -- let's do more of those!
  37.  
  38. The quick brown fox jumps over the lazy dog
  39.  
  40. abcdefghijklmnopqrstuvwxyz
  41. ABCDEFGHIJKLMNOPQRSTUVWXYZ
  42. 0123456789,:,;(*!?'")
  43. '''
  44.  
  45. def b64_zlib(data):
  46.     compressed = zlib.compress(data)
  47.     b64 = base64.encodestring(compressed)
  48.     return b64
  49.  
  50. def b64_zlib_decompress(b64):
  51.     b64 = base64.decodestring(b64)
  52.     data = StringIO(zlib.decompress(b64))
  53.     return data
  54.  
  55. def decrypt(message, key):
  56.     random.seed(key)
  57.     L = list(range(len(message)))
  58.     random.shuffle(L)
  59.     return "".join(message[i] for i, x in sorted(enumerate(L), key=lambda x: x[1]))
  60.    
  61. def encrypt(message, key):
  62.     random.seed(key)
  63.     L = range(len(message))
  64.     random.shuffle(L)
  65.     sss = "".join([message[x] for x in L])
  66.     return sss
  67.  
  68. def pyzip(code,func,depth=''):
  69.     L = len(code)
  70.     t = code
  71.     if depth:
  72.         t = func(t,depth)
  73.         print [the_key],
  74.         print L,
  75.     else:
  76.         t = func(t)
  77.     if len(t) <= L:
  78.         code = t
  79.     return code
  80.  
  81. code = msg
  82. for the_key in range(1, key_cycles):
  83.     ### code = pyzip(code,b64_zlib)
  84.     code = pyzip(code,encrypt,1)
  85.     print len(code)
  86.     print [code]
  87.     print
  88.  
  89.  
  90. for the_key in range(key_cycles-2,-1,-1):
  91.     ### code = pyzip(code,b64_zlib_decompress)
  92.     code = pyzip(code,decrypt,1)
  93.     print len(code)
  94.     print [code]
  95.     print
  96.     print
  97.    
  98. print code
Add Comment
Please, Sign In to add comment