Never been to CodeSnippets 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!)

2 total

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

2 total