-2

我正在尝试使用 mod 37 在 Python 中编写 Vigenere Cipher 程序。我需要帮助找出问题所在。

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)
4

1 回答 1

1

一个问题是您的Let2Ind()代码不能正确处理数字或空格。它将为数字(-49 或附近0)和空格(-65)返回一个负数。

你可能需要类似的东西:

def Let2Ind(x):
    return alphabet.index(x)
于 2014-10-31T02:20:30.453 回答