Python简单Vignere解密函数

Python简单Vignere解密函数,python,string,encryption,Python,String,Encryption,好的,我有作业。时间不早了,我的大脑功能不太好。 我的最终结果中有以下函数(但我必须使用它们): 最后这些是我的加密/解密器: def encryptVigne(key,string): cipher = '' keyLen = len(key) for i in range(len(string)): char = string[i] if char == ' ': cipher = cipher + char

好的,我有作业。时间不早了,我的大脑功能不太好。 我的最终结果中有以下函数(但我必须使用它们):

最后这些是我的加密/解密器:

def encryptVigne(key,string):
    cipher = ''
    keyLen = len(key)
    for i in range(len(string)):
        char = string[i]
        if char == ' ':
            cipher = cipher + char
        else:
            cipher = cipher + vigneIndex(key[i%keyLen], char)
    return cipher
加密机工作正常

nkey = 'abc'
print nkey
print encryptVigne(nkey, 'testing')
cip = encryptVigne(nkey, 'testing')

def undoVigne(key,cipher):
    string = ''
    keyLen = len(key)
    for i in range(len(cipher)):
        char = cipher[i]
        if char == ' ':
            string = string + char
        else:
            string = string + vigneIndex(key[i%keyLen], char)
    return string

但是解密并不能解密它。有人告诉我,这就像反转过程一样简单,但很明显,我遗漏了图片中的某些内容。

我没有测试它,但从飞行视图来看,您的函数
索引器
工作不正常。尝试通过以下方法更改它:

def indexToLetter(idx):
    az = "abcdefghijklmnopqrstuvwxyz"
    if idx > 25:
        print("err: ", idx, " too large")
        return ''
    elif idx < 0:
        print("err: ", idx , "should not be this")
        return ''
    else:
        return az[idx]
def索引器(idx):
az=“abcdefghijklmnopqrstuvxyz”
如果idx>25:
打印(“错误:,idx,“太大”)
返回“”
elif idx<0:
打印(“err:,idx,“不应该是这个”)
返回“”
其他:
返回az[idx]

但是如果
idx
错误,您应该引发异常,而不是将消息打印到输出流。

是否确实有代码对其进行解密

例如,在函数:
encryptVigne
中反转过程(解密)时,您不应该采取以下措施:

newIdx = (plainIdx - keyIdx) % 26
要反转加密时所做的操作,请执行以下操作:

newIdx = (plainIdx + keyIdx) % 26

谢谢我试过了,但没有。我有种感觉,我必须转换模式%26或类似的东西,但我现在睡着了
newIdx = (plainIdx - keyIdx) % 26
newIdx = (plainIdx + keyIdx) % 26