加密Vigenere ciph

2024-09-28 04:48:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我应该创建一个程序,使用一个字作为加密密钥,最终的行为是这样的

    $ 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

我得到了一大堆无效的语法错误贯穿始终,我对如何着手修复代码感到困惑。你知道吗

提前谢谢!你知道吗


Tags: thekeyinforstringifpositionencrypted
1条回答
网友
1楼 · 发布于 2024-09-28 04:48:02

最好的解决方法就是把每一个错误都记下来。例如,在if plaintextVal >= ord('z')中,在语句末尾需要一个:。然后,print Plaintext要求正确拼写变量plaintext。在修复所有语法错误之后,您可能会遇到更多错误,因为您正在尝试ord(plaintext),但不能获取字符串的ord,而只能获取字符

在我看来,在明文上循环而不是len(key)for plaintextCharacter in plaintext并跟踪每个plaintextCharacter要使用哪个键中的字符的不同计数器是有意义的

相关问题 更多 >

    热门问题