About this user

http://mike.hostetlerhome.com

« Newer Snippets
Older Snippets »
2 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

CircularStack

This class implements a sorta circular stack. If you get the top item, it will pop off but be added to the bottom.

class CircularStack(list):

    def gettop(self):
        item = self.pop()
        self.append(item)
        return item

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