有人能告诉我为啥我的加密有效但我的解密无效吗?看下面的图片

Posted

技术标签:

【中文标题】有人能告诉我为啥我的加密有效但我的解密无效吗?看下面的图片【英文标题】:Can someone tell me why my encrpyt works but not my decrpyt? look at picture below有人能告诉我为什么我的加密有效但我的解密无效吗?看下面的图片 【发布时间】:2015-11-10 17:50:21 【问题描述】:

#Vigenere 密码 加密消息="" 模式="" Keywordcount = 0 #将所有内容设置为 0

while Mode != "encrypt" and Mode != "decrypt":
    Mode = input('Do you want to encrypt or decrypt a message?')
    Mode = Mode.lower()

if Mode == "encrypt":

    Keyword=input('Please enter the keyword you want to use:')

    Message=input('Please eneter the message you witsh to encrypt: ')

    Keywordlength = len(Keyword)

    for i in range(0, len(Message)):#

            Messageencrypt = Message[i]
            Keywordencrypt = Keyword[Keywordcount]

            if Messageencrypt.isalpha():

                if Messageencrypt.isupper():

                    Messageencrypt = ord(Messageencrypt)

                    Keywordencrypt = Keywordencrypt.upper()

                    Keywordencrypt = ord(Keywordencrypt) - 64

                    CodedMessage = Messageencrypt + Keywordencrypt

                    if CodedMessage > 90:
                        CodedMessage = CodedMessage - 26

                    Encryptedletter = chr(CodedMessage)          


                elif Messageencrypt.islower():

                    Messageencrypt = ord(Messageencrypt)

                    Keywordencrypt = Keywordencrypt.upper()

                    Keywordencrypt = ord(Keywordencrypt) - 64

                    CodedMessage = Messageencrypt + Keywordencrypt

                    if CodedMessage > 122:
                        CodedMessage = CodedMessage - 26


                Encryptedletter = chr(CodedMessage)

            else:
                Encryptedletter = Messageencrypt

            EncryptedMessage = EncryptedMessage + Encryptedletter

            Keywordcount = Keywordcount + 1

            if Keywordcount >= Keywordlength:
                Keywordcount = 0

    print(EncryptedMessage)


if Mode == "decrypt":

    Keyword=input('Please enter the keyword you want to use:')

    Message=input('Please eneter the message you witsh to decrypt: ')

    Keywordlength = len(Keyword)

    for i in range(0, len(Message)):#

            Messageencrypt = Message[i]
            Keywordencrypt = Keyword[Keywordcount]

            if Messageencrypt.isalpha():

                if Messageencrypt.isupper():

                    Messageencrypt = ord(Messageencrypt)

                    Keywordencrypt = Keywordencrypt.upper()

                    Keywordencrypt = ord(Keywordencrypt) - 64

                    CodedMessage = Messageencrypt - Keywordencrypt

                    if CodedMessage > 90:
                        CodedMessage = CodedMessage - 26

                    Encryptedletter = chr(CodedMessage)          


                elif Messageencrypt.islower():

                    Messageencrypt = ord(Messageencrypt)

                    Keywordencrypt = Keywordencrypt.upper()

                    Keywordencrypt = ord(Keywordencrypt) - 64

                    CodedMessage = Messageencrypt - Keywordencrypt

                    if CodedMessage > 122:
                        CodedMessage = CodedMessage - 26


                Encryptedletter = chr(CodedMessage)

            else:
                Encryptedletter = Messageencrypt

            EncryptedMessage = EncryptedMessage + Encryptedletter

            Keywordcount = Keywordcount + 1

            if Keywordcount >= Keywordlength:
                Keywordcount = 0

    print(EncryptedMessage)

程序正确加密但不能正确解密 有人解释一下怎么回事

This image show what is what happens when i try to decrypt

【问题讨论】:

欢迎来到 Stack Overflow!您已经提供了代码,但没有显示它应该有什么问题。请edit您的问题提供示例输入、输出和预期输出。 【参考方案1】:

试试这个:

#Vigenere Cipher
EncryptedMessage=""
Mode=""
Keywordcount = 0 #sets everything to 0


while Mode != "encrypt" and Mode != "decrypt":
    Mode = input('Do you want to encrypt or decrypt a message?')
    Mode = Mode.lower()

if Mode == "encrypt":
    Keyword=input('Please enter the keyword you want to use:')
    Message=input('Please eneter the message you wish to encrypt: ')
    Keywordlength = len(Keyword)
    for i in range(0, len(Message)):
        Messageencrypt = Message[i]
        Keywordencrypt = Keyword[Keywordcount]
        if Messageencrypt.isalpha():
            if Messageencrypt.isupper():
                Messageencrypt = ord(Messageencrypt)
                Keywordencrypt = Keywordencrypt.upper()
                Keywordencrypt = ord(Keywordencrypt) - 64
                CodedMessage = Messageencrypt + Keywordencrypt
                if CodedMessage > 90:
                    CodedMessage = CodedMessage - 26
                Encryptedletter = chr(CodedMessage)
            elif Messageencrypt.islower():
                Messageencrypt = ord(Messageencrypt)
                Keywordencrypt = Keywordencrypt.upper()
                Keywordencrypt = ord(Keywordencrypt) - 64
                CodedMessage = Messageencrypt + Keywordencrypt
                if CodedMessage > 122:
                    CodedMessage = CodedMessage - 26
            Encryptedletter = chr(CodedMessage)
        else:
            Encryptedletter = Messageencrypt
        EncryptedMessage = EncryptedMessage + Encryptedletter
        Keywordcount = Keywordcount + 1
        if Keywordcount >= Keywordlength:
            Keywordcount = 0
    print(EncryptedMessage)


if Mode == "decrypt":
    Keyword=input('Please enter the keyword you want to use:')
    Message=input('Please eneter the message you wish to decrypt: ')
    Keywordlength = len(Keyword)
    for i in range(0, len(Message)):
        Messageencrypt = Message[i]
        Keywordencrypt = Keyword[Keywordcount]
        if Messageencrypt.isalpha():
            if Messageencrypt.isupper():
                Messageencrypt = ord(Messageencrypt)
                Keywordencrypt = Keywordencrypt.upper()
                Keywordencrypt = ord(Keywordencrypt) - 64
                CodedMessage = Messageencrypt - Keywordencrypt
                if CodedMessage < 65:
                    CodedMessage = CodedMessage + 26
                Encryptedletter = chr(CodedMessage)
            elif Messageencrypt.islower():
                Messageencrypt = ord(Messageencrypt)
                Keywordencrypt = Keywordencrypt.upper()
                Keywordencrypt = ord(Keywordencrypt) - 64
                CodedMessage = Messageencrypt - Keywordencrypt
                if CodedMessage < 97:
                    CodedMessage = CodedMessage + 26
            Encryptedletter = chr(CodedMessage)
        else:
            Encryptedletter = Messageencrypt
        EncryptedMessage = EncryptedMessage + Encryptedletter
        Keywordcount = Keywordcount + 1
        if Keywordcount >= Keywordlength:
            Keywordcount = 0
    print(EncryptedMessage)

这些是我在 diff 格式中所做的更改:

--- old.py  2015-11-10 14:01:31.000000000 -0500
+++ new.py  2015-11-10 13:56:52.000000000 -0500
@@ -54,7 +54,7 @@
Keywordencrypt = Keywordencrypt.upper()
Keywordencrypt = ord(Keywordencrypt) - 64
CodedMessage = Messageencrypt - Keywordencrypt
-                    if CodedMessage > 90:
+                    if CodedMessage < 65:
        CodedMessage = CodedMessage + 26
Encryptedletter = chr(CodedMessage)
elif Messageencrypt.islower():
@@ -62,7 +62,7 @@
Keywordencrypt = Keywordencrypt.upper()
Keywordencrypt = ord(Keywordencrypt) - 64
CodedMessage = Messageencrypt - Keywordencrypt
-                    if CodedMessage > 122:
+                    if CodedMessage < 97:
        CodedMessage = CodedMessage + 26
Encryptedletter = chr(CodedMessage)
else:

【讨论】:

我对 python 不太擅长,这对我来说需要一个月才能完成,所以我不太清楚你的意思,但是我爱你 比较我的版本中的第 24 和 57 行以及第 32 和 65 行。【参考方案2】:

您在加密和解密(即if CodedMessage &gt; 90:)中对CodedMessage 变量使用了更高的边界检查。这适用于加密,因为您希望编码消息 ASCII 十进制代码大于原来的值,因为您正在添加密钥加密。但是,对于您的解密,您不是在添加,而是在减去密钥:

CodedMessage = Messageencrypt - Keywordencrypt

这意味着您现在希望CodedMessage 比以前更小,这使得上限检查毫无用处。然后,您可以将代码更改为:

if CodedMessage < 65: #for upper letters
if CodedMessage < 97: #for lower letters

【讨论】:

以上是关于有人能告诉我为啥我的加密有效但我的解密无效吗?看下面的图片的主要内容,如果未能解决你的问题,请参考以下文章

为啥打印 unsigned char 有时有效,有时无效?在 C 中

为啥 localhost 有效但我的别名无效?

查询在 Access 中有效,但在 MySQL 中无效

我有两个被重命名为 jpg 的 gif。一个有效,一个无效。谁能告诉我为啥?

有人可以告诉我它是啥样的加密吗?

如何使用mcrypt解密