I'm trying to write a Vigenere Cipher program in Python using mod 37. I need help figuring out what the issue is.
alphabet= "abcdefghijklmnopqrstuvwxyz0123456789 "
def Let2Ind(x):
return(ord(x)-ord("a"))
def Ind2Let(n):
return(alphabet[n])
def encVigenere(key, plaintext):
ciphertext=""
L=len(key)
for i in range(len(plaintext)):
A=Let2Ind(key[i%L])
B=Let2Ind(plaintext[i])
C=(A+B)%37
D=Ind2Let(C)
ciphertext= ciphertext+D
return(ciphertext)
def decVigenere(key, ciphertext):
plaintext=""
L=len(key)
for i in range(len(ciphertext)):
E=Let2Ind(key[i%L])
F=Let2Ind(ciphertext[i])
G=(F-E)%37
H=Ind2Let(G)
plaintext= plaintext+H
return(plaintext)
One problem is that your Let2Ind()
code does not handle digits or space properly. It will return a negative number for the digits (-49 or thereabouts for 0
), and for space (-65).
You probably need something like:
def Let2Ind(x):
return alphabet.index(x)