Caesar Cipher 没有正确旋转字母? (Python)
Caesar Cipher not rotating letters properly? (Python)
我试图为我在学校的第一个 Python 项目制作凯撒密码。我从 youtube 视频中复制了主要密码段的代码,但是当我加密用户键入的消息时,它会使用随机密码而不是他输入 shell 的密钥。这是代码:
abc = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
def main():
message = input("What's the message to encrypt/decrypt? ")
key = int(input("What number would you like for your key value? "))
choice = input("Choose: encrypt or decrypt. ")
if choice == "encrypt":
encrypt(message, key)
elif choice == "decrypt":
encrypt(message, key * (-1))
else:
print("Bad answer, try again.")
def encrypt(message, key):
cipherText = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) + key) % 26
cipherText += abc[newPosition]
else:
cipherText += letter
print(cipherText)
return cipherText
main()
有人可以帮我解决这个问题吗?另外,请不要把它弄得太复杂,因为我是 Python 的初学者,我根本不懂。
谢谢!
abc = 'AaBbCcDdEeFfGgHhIiJjKKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
def main():
message = input("What's the message to encrypt/decrypt? ")
key = int(input("What number would you like for your key value? "))
choice = input("Choose: encrypt or decrypt. ")
if choice == "encrypt":
encrypt(message, key)
elif choice == "decrypt":
encrypt(message, key * (-1))
else:
print("Bad answer, try again.")
def encrypt(message, key):
cipherText = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) + key) % 26
cipherText += abc[newPosition]
else:
cipherText += letter
print(cipherText)
return cipherText
main()
缺少消息,加密中的关键参数
问题是您在字符集中将大写字母和小写字母交织在一起。所以当你试图用一个字符替换一个字符时,比如说,前面 5 个字符,你实际上是在翻转大小写并向前移动 2-3 个字符(取决于你开始的情况)。有更好的方法可以实现这一点,但事实证明,一个简单的更改就可以使您的代码按预期工作:
newPosition = (abc.find(letter) + key * 2) % 52
如果您在找到替换字符时按两次键,那么您将跳过字符集中的大写和小写字母。因为你的双键总是偶数,所以你最终会得到与开始时相同的情况。您还需要将模数相应地更改为 .
所指出的 52
虽然我同意@glibdud,但还有另一个错误。
您正在对键的值 + abc
中的位置取模 26。
但是 abc
是 52 个字符长 - 所以为了能够解密您加密的内容,您需要将其更改为 newPosition = (abc.find(letter) + key) % 52
如果你想让加密串更随意,比如包含一些标点符号或数字字符,将26或52替换为加密串的计算长度。
(abc.find(letter) + key) % 26
因为abc是大小写混合的。应用于字符 'C' 的键(例如:2)将导致 'D' 而不是 'E'。
我试图为我在学校的第一个 Python 项目制作凯撒密码。我从 youtube 视频中复制了主要密码段的代码,但是当我加密用户键入的消息时,它会使用随机密码而不是他输入 shell 的密钥。这是代码:
abc = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
def main():
message = input("What's the message to encrypt/decrypt? ")
key = int(input("What number would you like for your key value? "))
choice = input("Choose: encrypt or decrypt. ")
if choice == "encrypt":
encrypt(message, key)
elif choice == "decrypt":
encrypt(message, key * (-1))
else:
print("Bad answer, try again.")
def encrypt(message, key):
cipherText = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) + key) % 26
cipherText += abc[newPosition]
else:
cipherText += letter
print(cipherText)
return cipherText
main()
有人可以帮我解决这个问题吗?另外,请不要把它弄得太复杂,因为我是 Python 的初学者,我根本不懂。
谢谢!
abc = 'AaBbCcDdEeFfGgHhIiJjKKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
def main():
message = input("What's the message to encrypt/decrypt? ")
key = int(input("What number would you like for your key value? "))
choice = input("Choose: encrypt or decrypt. ")
if choice == "encrypt":
encrypt(message, key)
elif choice == "decrypt":
encrypt(message, key * (-1))
else:
print("Bad answer, try again.")
def encrypt(message, key):
cipherText = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) + key) % 26
cipherText += abc[newPosition]
else:
cipherText += letter
print(cipherText)
return cipherText
main()
缺少消息,加密中的关键参数
问题是您在字符集中将大写字母和小写字母交织在一起。所以当你试图用一个字符替换一个字符时,比如说,前面 5 个字符,你实际上是在翻转大小写并向前移动 2-3 个字符(取决于你开始的情况)。有更好的方法可以实现这一点,但事实证明,一个简单的更改就可以使您的代码按预期工作:
newPosition = (abc.find(letter) + key * 2) % 52
如果您在找到替换字符时按两次键,那么您将跳过字符集中的大写和小写字母。因为你的双键总是偶数,所以你最终会得到与开始时相同的情况。您还需要将模数相应地更改为
虽然我同意@glibdud,但还有另一个错误。
您正在对键的值 + abc
中的位置取模 26。
但是 abc
是 52 个字符长 - 所以为了能够解密您加密的内容,您需要将其更改为 newPosition = (abc.find(letter) + key) % 52
如果你想让加密串更随意,比如包含一些标点符号或数字字符,将26或52替换为加密串的计算长度。
(abc.find(letter) + key) % 26
因为abc是大小写混合的。应用于字符 'C' 的键(例如:2)将导致 'D' 而不是 'E'。