问题描述
我和我的朋友已经有一段时间像这样的“加密”对话了。该程序的目标是采用加密的消息并将其制成,以便任何人都可以读取它,反之亦然。 但是,如果我使用大写字母,则不会更改它们。 我该如何解决?
VOWELS = ["a","e","i","o","u"]
CONSONANTS = ["b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"]
def encrypt(message: str):
encryptedMessage = ""
for letter in message:
if letter in VOWELS:
newIndex = VOWELS.index(letter) + 1
if newIndex >= len(VOWELS):
newIndex = 0
encryptedMessage += VOWELS[newIndex]
elif letter in CONSONANTS:
newIndex = CONSONANTS.index(letter) + 1
if newIndex >= len(CONSONANTS):
newIndex = 0
encryptedMessage += CONSONANTS[newIndex]
else:
encryptedMessage += letter
return encryptedMessage
def decrypt(message: str):
decryptedMessage = ""
for letter in message:
if letter in VOWELS:
newIndex = VOWELS.index(letter) - 1
if newIndex >= len(VOWELS):
newIndex = 0
decryptedMessage += VOWELS[newIndex]
elif letter in CONSONANTS:
newIndex = CONSONANTS.index(letter) - 1
if newIndex >= len(CONSONANTS):
newIndex = 0
decryptedMessage += CONSONANTS[newIndex]
else:
decryptedMessage += letter
return decryptedMessage
def better_encryption(message: str,is_encrypt: bool):
newMessage = ""
ed = 1 if is_encrypt else -1
for letter in message:
if letter in VOWELS:
newIndex = VOWELS.index(letter) + ed
if newIndex >= len(VOWELS):
newIndex = 0
newMessage += VOWELS[newIndex]
elif letter in CONSONANTS:
newIndex = CONSONANTS.index(letter) + ed
if newIndex >= len(CONSONANTS):
newIndex = 0
newMessage += CONSONANTS[newIndex]
else:
newMessage += letter
return newMessage
def main():
while True:
command = str(input("[E]ncrypt or [D]ecrypt? "))
if "E" in command.upper() :
print(better_encryption(input("Message: "),True))
elif "D" in command.upper() :
print(better_encryption(input("Message: "),False))
else:
print("Invalid")
解决方法
在适当的位置使用简单的letter = letter.lower()
应该可以满足您的要求:
VOWELS = ["a","e","i","o","u"]
CONSONANTS = ["b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"]
def encrypt(message: str):
encryptedMessage = ""
for letter in message:
letter = letter.lower()
if letter in VOWELS:
newIndex = VOWELS.index(letter) + 1
if newIndex >= len(VOWELS):
newIndex = 0
encryptedMessage += VOWELS[newIndex]
elif letter in CONSONANTS:
newIndex = CONSONANTS.index(letter) + 1
if newIndex >= len(CONSONANTS):
newIndex = 0
encryptedMessage += CONSONANTS[newIndex]
else:
encryptedMessage += letter
return encryptedMessage
def decrypt(message: str):
decryptedMessage = ""
for letter in message:
letter = letter.lower()
if letter in VOWELS:
newIndex = VOWELS.index(letter) - 1
if newIndex >= len(VOWELS):
newIndex = 0
decryptedMessage += VOWELS[newIndex]
elif letter in CONSONANTS:
newIndex = CONSONANTS.index(letter) - 1
if newIndex >= len(CONSONANTS):
newIndex = 0
decryptedMessage += CONSONANTS[newIndex]
else:
decryptedMessage += letter
return decryptedMessage
def better_encryption(message: str,is_encrypt: bool):
newMessage = ""
ed = 1 if is_encrypt else -1
for letter in message:
letter = letter.lower()
if letter in VOWELS:
newIndex = VOWELS.index(letter) + ed
if newIndex >= len(VOWELS):
newIndex = 0
newMessage += VOWELS[newIndex]
elif letter in CONSONANTS:
newIndex = CONSONANTS.index(letter) + ed
if newIndex >= len(CONSONANTS):
newIndex = 0
newMessage += CONSONANTS[newIndex]
else:
newMessage += letter
return newMessage
def main():
while True:
command = str(input("[E]ncrypt or [D]ecrypt? "))
if "E" in command.upper() :
print(better_encryption(input("Message: "),True))
elif "D" in command.upper() :
print(better_encryption(input("Message: "),False))
else:
print("Invalid")
,
我还对您的代码进行了一些改进。
您可以类似地更改encrypt
和decrypt
。
VOWELS = [ord(c.upper()) for c in ["a","u"]]
CONSONANTS = [ord(c.upper()) for c in ["b","z"]]
# save letters' UPPER_CASE corresponding ascii number
LOWER_CONST = 0x20
UPPER_MASK = 0xff ^ LOWER_CONST
# we assume all strings are ascii
# in ascii,UPPER_CASE English letters are ORed with 0x20 (32)
# to produce lower_case letters
def better_encryption(message: str,is_encrypt: bool):
newMessage = ""
ed = 1 if is_encrypt else -1
for letter in message:
res = None
letter = ord(letter) # convert letter to corresponding ascii number
case = letter & LOWER_CONST # we save letter's sixth bit
letter &= UPPER_MASK # we set letter's sixth bit to zero,while keeping the other seven bits unchanged
if letter in VOWELS:
newIndex = (VOWELS.index(letter) + ed) % len(VOWELS) # mod operation handles cyclic shift
res = VOWELS[newIndex]
elif letter in CONSONANTS:
newIndex = (CONSONANTS.index(letter) + ed) % len(CONSONANTS) # mod operation handles cyclic shift
res = CONSONANTS[newIndex]
else:
res = letter # keep the letter unchanged
res |= case # restore letter's sixth bit
newMessage += chr(res) # convert res from number to corresponding ascii letter
return newMessage
def main():
while True:
command = input("[E]ncrypt or [D]ecrypt? ")
if len(command) == 0: # if user inputs empty string,we may not access the first character
continue
if command[0].upper() == 'E':
print(better_encryption(input("Message: "),True)) #encrypt the message
elif command[0].upper() == 'D':
print(better_encryption(input("Message: "),False)) #decrypt the message
else:
print("Invalid Command")
if __name__ == "__main__":
main()