python加密与解密

2024-10-02 14:20:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我怎么解决这个问题?它一直告诉我,我不能把{elif}函数放在{CodedLetter}之后,我明白为什么,但是我也不能把{if}函数放在后面,因为同样的事情发生了,我不知道还能做什么。有人知道我可以用什么来代替{while}或{elif}。

letters = "abcdefghijklmnopqrstuvwxyz" 
Welcome = print("Hello there...")
print("~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~")
print("¦        1. Encrypt message         ¦")
print("¦        2. Decrypt message         ¦")
print("¦             3. Exit               ¦")
print("~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~")

while Welcome is '1':
def Cipher(alphabet,UpperAlphabet,message,shift,FinalMessage):
    for x in message:
        if x in alphabet:
            CodedLetter = alphabet[(int(alphabet.index(x))+shift)%(len(alphabet))]
        FinalMessage += CodedLetter
    elif x in UpperAlphabet:
        CodedLetter = UpperAlphabet[(int(UpperAlphabet.index(x))+shift)%(len(UpperAlphabet))]
        FinalMessage += CodedLetter
    elif x == " ":
        CodedLetter = x
        FinalMessage += CodedLetter
return(FinalMessage)

alphabet = "".join(chr(x) for x in range(65,91))
UpperAlphabet = "".join(chr(x) for x in range(97,123))
message = input("Write your message")
shift = int(input("Enter shift amount"))
Word = []
FinalMessage = " "
print(Cipher(alphabet,UpperAlphabet,message,shift,FinalMessage))

while Welcome is '2':
def Cipher(alphabet,UpperAlphabet,message,shift,FinalMessage):
    for x in message:
        if x in alphabet:
            CodedLetter = alphabet[(int(alphabet.index(x))+shift)%(len(alphabet))]
        FinalMessage += CodedLetter
      elif x in UpperAlphabet:
        CodedLetter = UpperAlphabet[(int(UpperAlphabet.index(x))+shift)%(len(UpperAlphabet))]
         FinalMessage += CodedLetter
      elif x == " ":
      CodedLetter = x
        FinalMessage += CodedLetter
return(FinalMessage)

alphabet = "".join(chr(x) for x in range(65,91))
UpperAlphabet = "".join(chr(x) for x in range(97,123))
message = input("Write your message")
shift = int(input("Enter shift amount"))
Word = []
FinalMessage = " "
print(Cipher(alphabet,UpperAlphabet,message,shift,FinalMessage))

while welcome is '3':
exit()

Tags: inmessageforindexlenshiftintcipher
1条回答
网友
1楼 · 发布于 2024-10-02 14:20:10

你有很多重复的代码。。。我们来解决这个问题。。在

首先只为Caesar密码定义2个函数。一个用于加密和解密字符,另一个用于整个字符串。在

正数shift是加密,负数shift是解密。在

def cipher_character(character, shift):
    # Don't convert anything other than english characters
    if not character.isalpha():
        return character
    # declare some helping constants
    alpha_length = 26
    ascii_shift = ord('A') if character.isupper() else ord('a')
    cipher_shift = shift % alpha_length

    # shift down to 0..25 for a..z
    shifted = ord(character) - ascii_shift
    # rotate the letter and handle "wrap-around" for negatives and value >= 26
    shifted = (shifted + cipher_shift + alpha_length) % alpha_length
    # shift back up to english characters
    return chr(shifted + ascii_shift)

# Rotate a string k-positions
def cipher_string(string, shift):
    return ''.join(cipher_character(c, shift) for c in string)

接下来,提示用户输入

^{pr2}$

然后,对用户输入的选项进行一些if检查。在

if option == '1' or option == '2':
    message = input("Write your message: ")

    if option == '1':
        shift = int(input("Enter shift amount: "))
    else:
        shift = -1*int(input("Enter un-shift amount: "))

    print(cipher_string(message, shift))
elif option == '3':
    import sys
    sys.exit(0)

把所有这些放在一个文件中,用和这里相同的缩进,你应该很好。在


运行示例如下

$ python3 CeasarCipher.py
Hello there...
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
¦        1. Encrypt message         ¦
¦        2. Decrypt message         ¦
¦             3. Exit               ¦
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
1
Write your message: This is a test
Enter shift amount: 4
Xlmw mw e xiwx

$ python3 CeasarCipher.py
Hello there...
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
¦        1. Encrypt message         ¦
¦        2. Decrypt message         ¦
¦             3. Exit               ¦
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2
Write your message: Xlmw mw e xiwx
Enter un-shift amount: 4
This is a test

相关问题 更多 >