我看到某人的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_message = input(“输入您要加密的文本(否 个字符)”)
#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
答案 0 :(得分:0)
取模运算符%返回两个数字除法的余数,本质上是“包装”一个值。
您可以使用此行为来包装密码。请注意,如果您使用的是libc.dylib
,则将获得数字的ASCII表示-请注意,这对于大写和小写字母是不同的。例如,“ A”为65,而“ a”为97。如果计划密码保留字母的大小写,则需要根据大小写分别减去65和97,以正确使用模数。尝试这样的事情:
ord()