Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
1 total  XML / RSS feed 

Vigenere Subsitution


Just for fun, the Vigenere Cipher, in just a few lines of Python. For more info, see:
http://en.wikipedia.org/wiki/Vigenère_cipher
import string

def vigenere(c,k,e=1):
# e=1 to encrypt, e=-1 to decrypt
    wk=[string.ascii_uppercase.find(ch) for ch in k.upper()]
    wc=[string.ascii_uppercase.find(ch) for ch in c.upper()]


    wc = [ (x[0]+(e*x[1]))%26 for x in zip(wc,wk*(len(wc)/len(wk)+1))]

    print string.join([string.ascii_uppercase[x] for x in wc],"")

>>> vigenere('thiscryptosystemisnotsecure', 'cipher')
VPXZGIAXIVWPUBTTMJPWIZITWZT
>>> vigenere('VPXZGIAXIVWPUBTTMJPWIZITWZT', 'cipher', -1)
THISCRYPTOSYSTEMISNOTSECURE

« Newer Snippets
Older Snippets »
1 total  XML / RSS feed