有人能告诉我为什么我的加密有效但我的解密无效吗?看下面的图片
Can someone tell me why my encrpyt works but not my decrpyt? look at picture below
#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)
程序加密正确但解密不正确
有人解释一下哪里错了
您在加密和解密(即 if CodedMessage > 90:
)中对 CodedMessage
变量使用了更高的边界检查。这适用于加密,因为您希望编码的消息 ASCII 十进制代码比原来的更大,因为您正在添加 keyencrypt。但是,对于您的解密,您不是在添加,而是在减去密钥:
CodedMessage = Messageencrypt - Keywordencrypt
这意味着您现在期望 CodedMessage
比以前更小,这使得更高的边界检查变得无用。然后您可以将代码更改为:
if CodedMessage < 65: #for upper letters
if CodedMessage < 97: #for lower letters
试试这个:
#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)
这些是我在差异格式中所做的更改:
--- 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:
#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)
程序加密正确但解密不正确 有人解释一下哪里错了
您在加密和解密(即 if CodedMessage > 90:
)中对 CodedMessage
变量使用了更高的边界检查。这适用于加密,因为您希望编码的消息 ASCII 十进制代码比原来的更大,因为您正在添加 keyencrypt。但是,对于您的解密,您不是在添加,而是在减去密钥:
CodedMessage = Messageencrypt - Keywordencrypt
这意味着您现在期望 CodedMessage
比以前更小,这使得更高的边界检查变得无用。然后您可以将代码更改为:
if CodedMessage < 65: #for upper letters
if CodedMessage < 97: #for lower letters
试试这个:
#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)
这些是我在差异格式中所做的更改:
--- 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: