-2

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)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Cfish
  • 1
  • 1
  • Welcome to Stack Overflow. Please read the [About] page soon. It would be sensible if you showed us some sample input (key and plain text) and the expected (encrypted) output. We would assume that the decryption should reveal with original text. You should also show what you do get, and explain why it is wrong. – Jonathan Leffler Oct 31 '14 at 02:16

1 Answers1

1

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)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278