Python 将用户输入转换为适合密码后的字母移位
我不是python的高手,我正在尝试对用户输入的移位文本进行加密。该密码的工作方式是忽略符号、数字等。它还将句号转换为X,并且必须全部为大写。我目前有代码,但不确定如何采取转换后的文本,并转移由用户给定的数字它。不确定这一切是否有意义,但任何帮助都将不胜感激 这是我的密码:Python 将用户输入转换为适合密码后的字母移位,python,encryption,shift,Python,Encryption,Shift,我不是python的高手,我正在尝试对用户输入的移位文本进行加密。该密码的工作方式是忽略符号、数字等。它还将句号转换为X,并且必须全部为大写。我目前有代码,但不确定如何采取转换后的文本,并转移由用户给定的数字它。不确定这一切是否有意义,但任何帮助都将不胜感激 这是我的密码: def convert_to_Caesar(t): #Remove all special characters and only show A-Z t = re.sub("[^A-
def convert_to_Caesar(t):
#Remove all special characters and only show A-Z
t = re.sub("[^A-Za-z.]",'', t)
cipherText = ""
# Full stops are replaced with X's
for letter in t:
if letter == '.':
cipherText += 'X'
# Lower case is converted to upper case
else:
cipherText += letter.upper()
# Plain text is ciphered and returned
return cipherText
# User enters plain text to cipher
text = input("What do you want to cipher? ")
shift = int(input("How many positions to shift by? "))
print(convert_to_Caesar(text))
谢谢您可以按照@Girish Srivatsa的建议使用ord()/chr():
alphabet_len = ord('Z') - ord('A') + 1
new_letter = chr((ord(letter.upper()) - ord('A') + shift) % alphabet_len + ord('A'))
但如果您只创建一个保存字母表的变量,它可能会更干净:
import string
alphabet = "".join(list(string.ascii_uppercase))
然后查找字母表中的位置,添加位置,然后查找新字母:
pos = alphabet.find(letter.upper())
if pos == -1:
if letter == '.':
new_letter = 'X'
else:
# discard other symbols
new_letter = ''
else:
new_pos = (pos + shift) % len(alphabet)
new_letter = alphabet[new_pos]
注意,您无法判断密文中的“X”是移位字母还是“.”。如果需要解决此问题,请将“.”添加到字母表中,并在pos=-1下删除“.”的特殊大小写。使用chr()/ord()方法时,这会变得混乱。如果您希望它的格式更加正确,请遵循所有规则,但也要使用大写和小写格式。这将移动字典,并运行if循环。我知道你要求所有字母都是大写,但这会稍微改进代码 代码输出:
Do you want to... 1. Encode, or 2. Decode? 1
This must be encoded! Please, Work!
5
Ymnx rzxy gj jshtiji! Uqjfxj, Btwp!
代码:
为什么不使用ord、chr的ASCII码?
import time
def shift_dict(Caesar, Shift):
dic_len = len(Caesar)
Shift = Shift % dic_len
list_dic = [(k,v) for k, v in iter(Caesar.items())]
Shifted = {
list_dic[x][0]: list_dic[(x - Shift) % dic_len][1]
for x in range(dic_len)
}
return Shifted
def shift_dict2(Caesar, Shift):
dic_len = len(Caesar)
Shift = Shift % dic_len
list_dic = [(k,v) for k, v in iter(Caesar.items())]
Shifted = {
list_dic[x][0]: list_dic[(x - Shift) % dic_len][-1]
for x in range(dic_len)
}
return Shifted
UpperList = {
"A":0,
"B":1,
"C":2,
"D":3,
"E":4,
"F":5,
"G":6,
"H":7,
"I":8,
"J":9,
"K":10,
"L":11,
"M":12,
"N":13,
"O":14,
"P":15,
"Q":16,
"R":17,
"S":18,
"T":19,
"U":20,
"V":21,
"W":22,
"X":23,
"Y":24,
"Z":25
}
UpperCaesar = {
"A":"A",
"B":"B",
"C":"C",
"D":"D",
"E":"E",
"F":"F",
"G":"G",
"H":"H",
"I":"I",
"J":"J",
"K":"K",
"L":"L",
"M":"M",
"N":"N",
"O":"O",
"P":"P",
"Q":"Q",
"R":"R",
"S":"S",
"T":"T",
"U":"U",
"V":"V",
"W":"W",
"X":"X",
"Y":"Y",
"Z":"Z"
}
LowerList = {
"a":0,
"b":1,
"c":2,
"d":3,
"e":4,
"f":5,
"g":6,
"h":7,
"i":8,
"j":9,
"k":10,
"l":11,
"m":12,
"n":13,
"o":14,
"p":15,
"q":16,
"r":17,
"s":18,
"t":19,
"u":20,
"v":21,
"w":22,
"x":23,
"y":24,
"z":25
}
LowerCaesar = {
"a":"a",
"b":"b",
"c":"c",
"d":"d",
"e":"e",
"f":"f",
"g":"g",
"h":"h",
"i":"i",
"j":"j",
"k":"k",
"l":"l",
"m":"m",
"n":"n",
"o":"o",
"p":"p",
"q":"q",
"r":"r",
"s":"s",
"t":"t",
"u":"u",
"v":"v",
"w":"w",
"x":"x",
"y":"y",
"z":"z"
}
UpperList1 = {
"A":0,
"B":1,
"C":2,
"D":3,
"E":4,
"F":5,
"G":6,
"H":7,
"I":8,
"J":9,
"K":10,
"L":11,
"M":12,
"N":13,
"O":14,
"P":15,
"Q":16,
"R":17,
"S":18,
"T":19,
"U":20,
"V":21,
"W":22,
"X":23,
"Y":24,
"Z":25
}
UpperCaesar1 = {
"A":"A",
"B":"B",
"C":"C",
"D":"D",
"E":"E",
"F":"F",
"G":"G",
"H":"H",
"I":"I",
"J":"J",
"K":"K",
"L":"L",
"M":"M",
"N":"N",
"O":"O",
"P":"P",
"Q":"Q",
"R":"R",
"S":"S",
"T":"T",
"U":"U",
"V":"V",
"W":"W",
"X":"X",
"Y":"Y",
"Z":"Z"
}
LowerList1 = {
"a":0,
"b":1,
"c":2,
"d":3,
"e":4,
"f":5,
"g":6,
"h":7,
"i":8,
"j":9,
"k":10,
"l":11,
"m":12,
"n":13,
"o":14,
"p":15,
"q":16,
"r":17,
"s":18,
"t":19,
"u":20,
"v":21,
"w":22,
"x":23,
"y":24,
"z":25
}
LowerCaesar1 = {
"a":"a",
"b":"b",
"c":"c",
"d":"d",
"e":"e",
"f":"f",
"g":"g",
"h":"h",
"i":"i",
"j":"j",
"k":"k",
"l":"l",
"m":"m",
"n":"n",
"o":"o",
"p":"p",
"q":"q",
"r":"r",
"s":"s",
"t":"t",
"u":"u",
"v":"v",
"w":"w",
"x":"x",
"y":"y",
"z":"z"
}
Asker = int(input("Do you want to... 1. Encode, or 2. Decode? "))
if Asker == 1:
Plaintext = str(input(""))
OriginalShift = int(input(""))
Shift = OriginalShift*-1
UpperCaesar = shift_dict(UpperCaesar, Shift)
LowerCaesar = shift_dict(LowerCaesar, Shift)
Lister = []
X = 0
for i in range(len(Plaintext)):
if Plaintext[X].isalpha():
if Plaintext[X].isupper():
Lister.append(UpperCaesar[Plaintext[X]])
else:
Lister.append(LowerCaesar[Plaintext[X]])
else:
Lister.append(Plaintext[X])
X += 1
print(*Lister, sep = "")
elif Asker == 2:
Asker1 = int(input("Do you have the key (1), or not(2): "))
if Asker1 == 1:
Plaintext = str(input(""))
OriginalShift = int(input(""))
Shift = OriginalShift*-1
UpperCaesar = shift_dict(UpperCaesar, 26 - Shift)
LowerCaesar = shift_dict(LowerCaesar, 26 - Shift)
Lister = []
X = 0
for i in range(len(Plaintext)):
if Plaintext[X].isalpha():
if Plaintext[X].isupper():
Lister.append(UpperCaesar[Plaintext[X]])
else:
Lister.append(LowerCaesar[Plaintext[X]])
else:
Lister.append(Plaintext[X])
X += 1
print(*Lister, sep = "")
elif Asker1 == 2:
Plaintext = str(input(""))
OriginalShift = 0
for i in range(26):
UpperCaesar = shift_dict2(UpperCaesar, -1)
LowerCaesar = shift_dict2(LowerCaesar, -1)
Lister = []
X = 0
for i in range(len(Plaintext)):
if Plaintext[X].isalpha():
if Plaintext[X].isupper():
Lister.append(UpperCaesar[Plaintext[X]])
else:
Lister.append(LowerCaesar[Plaintext[X]])
else:
Lister.append(Plaintext[X])
X += 1
time.sleep(0.01)
print("With a shift of ", 25 - (OriginalShift*-1), ": ", *Lister, sep = "")
OriginalShift -= 1