【问题标题】:Vigenere Cipher program in Python using mod 37使用 mod 37 的 Python 中的 Vigenere Cipher 程序
【发布时间】:2014-10-31 02:10:38
【问题描述】:

我正在尝试使用 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)

【问题讨论】:

  • 欢迎来到 Stack Overflow。请尽快阅读About 页面。如果您向我们展示一些示例输入(密钥和纯文本)和预期的(加密)输出,那将是明智的。我们假设解密应该显示原始文本。你还应该展示你得到了什么,并解释为什么它是错误的。

标签: python encryption vigenere


【解决方案1】:

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

你可能需要这样的东西:

def Let2Ind(x):
    return alphabet.index(x)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多