这是一个可以加密和解密的代码。它甚至返回格式!(例如,资本,较低)
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