Skip to content

Instantly share code, notes, and snippets.

@scturtle
Created November 21, 2012 03:24
Show Gist options
  • Save scturtle/4122814 to your computer and use it in GitHub Desktop.
Save scturtle/4122814 to your computer and use it in GitHub Desktop.
simple encryption
from math import ceil
from itertools import izip_longest
def encrypt(s, w):
n = len(s)
l = [s[i:i + w] for i in xrange(0, n, w)]
es = [''.join(t) for t in izip_longest(*l, fillvalue='')]
return str(w) + ''.join(es)
def decrypt(s):
k = int(s[0])
s = s[1:]
n = len(s)
w = int(ceil(1. * n / k))
full = k - (k * w - n)
l, st = [], 0
for i in xrange(k):
ww = w if i < full else w - 1
l.append(s[st:st + ww])
st += ww
es = [''.join(t) for t in izip_longest(*l, fillvalue='')]
return ''.join(es)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment