加密Vigenere密码

问题描述 投票:0回答:1

我应该创建一个程序,使用一个单词作为加密密钥,最终表现得像这样

    $ python vigenere.py
Type a message:
The crow flies at midnight!
Encryption key:
boom
Uvs osck rmwse bh auebwsih!

这是Vigenere cipher的链接

我的主要问题是它的加密方面。

def vigenere_cipher(plaintext, key):
encrypted_string = ''
for i in range(len(key)):
# loop over plaintext by index
    plaintextVal = ord(plaintext)
    # if character in plaintext is alpha do the following:
    keyVal = ord(key[i])
    keyVal = keyVal - 97
    # get alphabet position for character in key

    plaintextChar += chr(keyVal + plaintextVal)
    if plaintextVal >= ord('z')
    #get alphabet position for character in plaintext
        plaintextVal = plaintextVal - 26
        # rotate character from plaintext with a character from the key 
        # convert new position back to a character if you haven't done it already
        # concatenate new character to an encrypted string             


        print PlaintextVal

return encrypted_string

我在整个过程中遇到了一堆无效的语法错误,我对如何修复代码感到困惑。

提前致谢!

python vigenere
1个回答
0
投票

解决问题的最佳方法是随身携带每个错误。例如,在if plaintextVal >= ord('z')中,你需要在声明的末尾加上:。然后,print Plaintext要求变量plaintext正确拼写。修复所有语法错误后,您可能会遇到更多错误,因为您正在尝试使用ord(plaintext),但是您不能使用字符串的ord,而只能使用字符。

在我看来,你可以循环使用明文而不是len(key)for plaintextCharacter in plaintext并跟踪一个不同的计数器,以便为每个plaintextCharacter处理密钥中的哪个字符。

© www.soinside.com 2019 - 2024. All rights reserved.