检查符号和 upper/lower-case 以使用凯撒密码进行加密?
Checking for symbols and upper/lower-case for encrypting with ceasar cipher?
我尝试了几个小时来制作一个简单的 ceasar 密码加密程序。它终于奏效了。但它只能加密没有空格和符号的文本。我不知道如何将这些东西的检查部分实现到代码中。对使代码更清洁和 DRY 的批评者表示赞赏。谢谢
我尝试实现功能的代码:
#Taking Input String + converting to list
message = input("Enter the message for encrypting: ")
message_list = list(message)
#Taking Input Cipher
cipher = int(input("Enter shifting value (1-26): "))
#Alphabet
alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#Defining variable
encrypted_message = []
#Shifting
for letter in message_list:
if letter not in alphabet:
encrypted_message.append(letter)
else:
#Getting Index of the letter in alphabet
letter_position = alphabet.index(letter)
#Getting the shifting value for the letter
shifting_value = letter_position + cipher
#Getting the shifted letter
shifted_letter = alphabet[shifting_value]
#Adding the corresponding letter to the encrypted message
encrypted_message.append(shifted_letter)
#Output
print("Original Message: " + message)
print("Encrypted Message: " + str(encrypted_message))
print("Cipher: " + str(cipher))
它应该做什么:
用空格和符号加密消息(凯撒密码),
它在做什么:
发生异常:IndexError
列表索引超出范围
IndexError list index out of range
错误是因为您忘记了模数。没有模数,
shifting_value = letter_position + cipher
此行的值将高于您正在编制索引的数组。记住,凯撒密码是
c = p + 3 mod 26
所以该行必须是
shifting_value = (letter_position + 密码) % 26
注意 1:密码在这里不是一个好的变量名。应该是shiftAmount.
注2:如果要将列表合并成字符串,使用
str1 = ''.join(encrypted_message)
考虑到给出的评论,这是一个可以说更简洁的方法。
#Taking Input String + converting to list
message = input("Enter the message for encrypting: ")
message_list = list(message)
#Taking Input Cipher
cipher = int(input("Enter shifting value (1-26): "))
#Defining variable
encrypted_message = ''
#Shifting
for letter in message_list:
code_point = ord(letter)
if code_point > ord('z') or code_point < ord('a') :
encrypted_message += letter
else:
#Getting Index of the letter in alphabet
letter_position = code_point - ord('a')
#Getting the shifting value for the letter
shifting_value = (letter_position + cipher) % 26
#Getting the shifted letter
shifted_letter = chr(ord('a') + shifting_value)
#Adding the corresponding letter to the encrypted message
encrypted_message += shifted_letter
#Output
print("Original Message: " + message)
print("Encrypted Message: " + str(encrypted_message))
print("Cipher: " + str(cipher))
当然可以把ord('a') 和ord('z') 换成各自的值这样就不用计算了,但是有利于理解
Python 2.7 用raw_input
替换输入
原来的工作版本:
#Taking Input String + converting to list
message = input("Enter the message for encrypting: ")
message_list = list(message)
#Taking Input Cipher
cipher = int(input("Enter shifting value (1-26): "))
#Alphabet
alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#Defining variable
encrypted_message = ''
#Shifting
for letter in message_list:
if letter not in alphabet:
encrypted_message += letter
else:
#Getting Index of the letter in alphabet
letter_position = alphabet.index(letter)
#Getting the shifting value for the letter
shifting_value = (letter_position + cipher) % 26
#Getting the shifted letter
shifted_letter = alphabet[shifting_value]
#Adding the corresponding letter to the encrypted message
encrypted_message += shifted_letter
#Output
print("Original Message: " + message)
print("Encrypted Message: " + str(encrypted_message))
print("Cipher: " + str(cipher))
我尝试了几个小时来制作一个简单的 ceasar 密码加密程序。它终于奏效了。但它只能加密没有空格和符号的文本。我不知道如何将这些东西的检查部分实现到代码中。对使代码更清洁和 DRY 的批评者表示赞赏。谢谢
我尝试实现功能的代码:
#Taking Input String + converting to list
message = input("Enter the message for encrypting: ")
message_list = list(message)
#Taking Input Cipher
cipher = int(input("Enter shifting value (1-26): "))
#Alphabet
alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#Defining variable
encrypted_message = []
#Shifting
for letter in message_list:
if letter not in alphabet:
encrypted_message.append(letter)
else:
#Getting Index of the letter in alphabet
letter_position = alphabet.index(letter)
#Getting the shifting value for the letter
shifting_value = letter_position + cipher
#Getting the shifted letter
shifted_letter = alphabet[shifting_value]
#Adding the corresponding letter to the encrypted message
encrypted_message.append(shifted_letter)
#Output
print("Original Message: " + message)
print("Encrypted Message: " + str(encrypted_message))
print("Cipher: " + str(cipher))
它应该做什么: 用空格和符号加密消息(凯撒密码),
它在做什么: 发生异常:IndexError 列表索引超出范围
IndexError list index out of range
错误是因为您忘记了模数。没有模数,
shifting_value = letter_position + cipher
此行的值将高于您正在编制索引的数组。记住,凯撒密码是
c = p + 3 mod 26
所以该行必须是
shifting_value = (letter_position + 密码) % 26
注意 1:密码在这里不是一个好的变量名。应该是shiftAmount.
注2:如果要将列表合并成字符串,使用
str1 = ''.join(encrypted_message)
考虑到给出的评论,这是一个可以说更简洁的方法。
#Taking Input String + converting to list
message = input("Enter the message for encrypting: ")
message_list = list(message)
#Taking Input Cipher
cipher = int(input("Enter shifting value (1-26): "))
#Defining variable
encrypted_message = ''
#Shifting
for letter in message_list:
code_point = ord(letter)
if code_point > ord('z') or code_point < ord('a') :
encrypted_message += letter
else:
#Getting Index of the letter in alphabet
letter_position = code_point - ord('a')
#Getting the shifting value for the letter
shifting_value = (letter_position + cipher) % 26
#Getting the shifted letter
shifted_letter = chr(ord('a') + shifting_value)
#Adding the corresponding letter to the encrypted message
encrypted_message += shifted_letter
#Output
print("Original Message: " + message)
print("Encrypted Message: " + str(encrypted_message))
print("Cipher: " + str(cipher))
当然可以把ord('a') 和ord('z') 换成各自的值这样就不用计算了,但是有利于理解
Python 2.7 用raw_input
替换输入原来的工作版本:
#Taking Input String + converting to list
message = input("Enter the message for encrypting: ")
message_list = list(message)
#Taking Input Cipher
cipher = int(input("Enter shifting value (1-26): "))
#Alphabet
alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#Defining variable
encrypted_message = ''
#Shifting
for letter in message_list:
if letter not in alphabet:
encrypted_message += letter
else:
#Getting Index of the letter in alphabet
letter_position = alphabet.index(letter)
#Getting the shifting value for the letter
shifting_value = (letter_position + cipher) % 26
#Getting the shifted letter
shifted_letter = alphabet[shifting_value]
#Adding the corresponding letter to the encrypted message
encrypted_message += shifted_letter
#Output
print("Original Message: " + message)
print("Encrypted Message: " + str(encrypted_message))
print("Cipher: " + str(cipher))