Skip to content

Instantly share code, notes, and snippets.

@jamak
Created August 12, 2012 15:55
Show Gist options
  • Select an option

  • Save jamak/3332447 to your computer and use it in GitHub Desktop.

Select an option

Save jamak/3332447 to your computer and use it in GitHub Desktop.
import random
def enc(s):
n = 0
for c in s:
n = (n<<1)+random.randint(0, 1)
n = (n<<8)+ord(c)
return -1 * (~(n<<4))
def dec(n):
n = (~(-1 * n))>>4
s = ''
while n > 0:
s = chr(n&0xff) + s
n >>= 9
return s
def test(s):
n = enc(s)
print(s, n, dec(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment