加密Vigenere密码

时间:2018-08-31 02:47:08

标签: python vigenere

我应该创建一个程序,该程序使用单词作为最终的加密行为

    $ 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

整个过程中我都收到了很多无效的语法错误,我对如何修复代码感到困惑。

谢谢!

1 个答案:

答案 0 :(得分:0)

解决此问题的最佳方法是随手处理每个错误。例如,在if plaintextVal >= ord('z')中,语句末尾需要一个:。然后,print Plaintext要求变量plaintext的拼写正确。在纠正所有语法错误之后,您可能会遇到更多错误,因为您正在尝试ord(plaintext),但不能仅将字符串作为字符串的ord

在我看来,对您来说,遍历明文而不是len(key)for plaintextCharacter in plaintext并跟踪要使用其键中字符的计数器是有意义的每个plaintextCharacter