首页 > 解决方案 > 如何重复字母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 时它会继续运行:^_`

标签: pythonencryption

解决方案


模运算符 % 返回两个数字相除的余数,本质上是“环绕”一个值。

您可以使用此行为来包装您的密码。请注意,如果您使用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"))

在这里试试!


推荐阅读