Created
November 21, 2012 03:24
-
-
Save scturtle/4122814 to your computer and use it in GitHub Desktop.
simple encryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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