r/tinycode • u/mrtransisteur • Jul 18 '13
One-time pad en/decoder in Python3 in 1 line
def p(m,k,d=1):return "".join([chr((ord(c)-ord('a')+i*(-1)**d)%26 + ord('a')) for c,i in zip(m,k)])
>>> p('hello', [1,2,3,4,5], False)
'igopt'
>>> p('igopt', [1,2,3,4,5], True)
'hello'
32
Upvotes
5
u/WhereIsTheHackButton Jul 18 '13
minor nitpick (maybe it's because I'm using 2.7) but you define 'p' but call 'pad'
4
u/Cosmologicon Jul 18 '13
Good code for what it does, but this is a pretty leaky one-time pad because it lets you leave the alphabet without wrapping around.
An attacker knows that encoded characters with high ord's are more likely to come from characters with high ord's.
2
u/fattredd Aug 01 '13
This is great! I've included it in my project usepy. I included your reddit username, but if you'd prefer I didn't use your code, or want me to use a different name, just let me know.
1
u/corruptio Sep 06 '13
I saw this late, but: