python - 如何重复字母Ceaser Cipher
问题描述
我看到了某人的 Ceaser 密码问题,并尝试自己编写。我完成了所有事情,除了我的字母表需要环绕
#User input
user_message = input("Input the text you would like encrypted (no
characters)")
#Cipher function
def Ciphertext(message):
cipher = ""
for i in message:
#takes each letter in message
number_code = ord(i) + 3
letter_code = chr(number_code)
cipher = cipher + letter_code
return cipher
#Unencrypted function
def Plaintext(cipher):
text = ""
#loops to get each number
for i in cipher:
#takes each letter in cipher
unencrypted_code = ord(i) - 3
unencrypted_letter_code = chr(unencrypted_code)
text = text + unencrypted_letter_code
print(text)
#Prints
print("Encrypted message")
print(Ciphertext(user_message))
print("Unencrypted message")
Plaintext(Ciphertext(user_message))
好的,所以我将代码更改为:#User input user_message = input("Input the text you want encrypted (no characters)")
#Cipher function
def Ciphertext(message):
cipher = ""
for i in message:
#takes each letter in message then coverts it to number subtracts the
diffrence then converts it back into characters
number_code = ord(i) + 3
letter_code = chr(number_code)
if number_code >= ord("z"):
number_code = number_code - 123
number_code = number_code + ord("a")
letter_code = chr(number_code)
cipher = cipher + letter_code
return cipher
cipher = Ciphertext(user_message)
#Unencrypted function
def Plaintext():
text = ""
#loops to get each number
for i in cipher:
#takes each letter in cipher
unencrypted_code = ord(i) - 3
if unencrypted_code >= ord("z"):
unencryted_code = unencrypted_code + 26
unencrypted_letter_code = chr(unencrypted_code)
text = text + unencrypted_letter_code
print(text)
#Prints
print("Encrypted message")
print(Ciphertext(user_message))
print("Unencrypted message")
Plaintext()
但是当它输入 xyz 时它会继续运行:^_`
解决方案
模运算符 % 返回两个数字相除的余数,本质上是“环绕”一个值。
您可以使用此行为来包装您的密码。请注意,如果您使用ord()
,您将获得数字的 ASCII 表示 - 请注意,这对于大写和小写字母是不同的。例如,“A”是 65,“a”是 97。如果您计划让密码保留字母的大小写,则需要根据大小写减去 65 和 97,才能正确使用模数。尝试这样的事情:
def Ciphertext(message):
cipher = ""
for i in message:
#determine if this is an uppercase or lowercase character, and treat it accordingly
number_code = 0
if i.isupper():
number_code = (((ord(i) - 65) + 3) % 26) + 65
elif i.islower():
number_code = (((ord(i) - 97) + 3) % 26) + 97
else:
number_code = ord(i)
letter_code = chr(number_code)
cipher += letter_code
return cipher
def Plaintext(cipher):
text = ""
for i in cipher:
unencrypted_code = 0
if i.isupper():
unencrypted_code = (((ord(i) - 65) - 3) % 26) + 65
elif i.islower():
unencrypted_code = (((ord(i) - 97) - 3) % 26) + 97
else:
unencrypted_code = ord(i)
letter_code = chr(unencrypted_code)
text += letter_code
return text
print (Ciphertext("Hello world! wxyz"))
print (Plaintext("Khoor zruog! zabc"))
推荐阅读
- javascript - 使用干净的 JavaScript 堆配置文件在长时间运行的 THREE.js 应用程序中的 GPU 崩溃
- python - 使用python合并相似的xml文件
- java - JavaFX TableView 更新单元格,不更新对象值
- c++ - 如何将 arm64 中 vaddv_u8 的结果视为霓虹灯寄存器
- python-asyncio - Python:通过完成订购任务后获取对原始任务的引用
- html - css 将父 div 扩展到子高度
- angular - 如何以角度4填充选定的下拉文本
- laravel - 多对多。在将附加列保存到数据库时出错
- php - PhpMailer 发送的电子邮件进入垃圾邮件,也在发送的安全详细信息中显示我的主机参数,如何解决这个问题?
- jquery - Jquery 验证器表单