Skip to content

Instantly share code, notes, and snippets.

@sammyrulez
Created December 14, 2011 09:35
Show Gist options
  • Save sammyrulez/1475884 to your computer and use it in GitHub Desktop.
Save sammyrulez/1475884 to your computer and use it in GitHub Desktop.
Vigenère cipher python functions
from string import ascii_lowercase
def buildmap():
table = {}
for x in range(0,len(ascii_lowercase)):
row = {}
c = 0
for y in range(0,len(ascii_lowercase)):
row[ascii_lowercase[y]] = ascii_lowercase[x+y+c]
if x+y > 24:
c = -26
table[ascii_lowercase[x]] = row
return table
def build_mask(password,lenx):
mask = ""
x = 0
c = 0
while x < lenx:
mask = mask + password[c]
x = x + 1
c = c + 1
if c >= len(password):
c = 0
return mask
def crypt(clear,password):
mask = build_mask(password,len(clear))
out = ""
low_clear = clear.lower()
table = buildmap()
for x in range(0,len(clear)):
out = out + table[mask[x]][low_clear[x]]
return out
def decrypt(hashed,password):
mask = build_mask(password,len(hashed))
out = ""
low_hashed = hashed.lower()
table = buildmap()
for x in range(0,len(low_hashed)):
for c in table[mask[x]]:
if table[mask[x]][c] == low_hashed[x]:
out = out + c
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment