def getMode():
while True:
print('请选择加密或解密模式,或者选择暴力破解')
print('加密:encrypt(e)')
print('解密:decrypt(d)')
print('暴力破解:brute(b)')
mode = input().lower()
if mode in 'encrypt e decrypt d brute b'.split():
return mode
else:
print('请输入"encrypt"或"e"或"decrypt"或"d"或"brute"或"b"!')
def getKey():
key = 0
while True:
print('请输入密钥数字(1-%s)'%(MAX_KEY_SIZE))
key = int(input())
if (key >= 1 and key <= MAX_KEY_SIZE):
return key
def getTranslatedMessage(mode,message,key):
if mode[0] == 'd':
key = -key
translated = ''
for symbol in message:
if symbol.isalpha():
num = ord(symbol)
num += key
if symbol.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
elif symbol.islower():
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
translated += chr(num)
else:
translated += symbol
return translated
mode = getMode()
message = getMessage()
if mode[0] != 'b':
key = getKey()
print('你要翻译的信息是:')
if mode[0] != 'b':
print(getTranslatedMessage(mode,message,key))
else:
for key in range(1,MAX_KEY_SIZE + 1):
print(key,getTranslatedMessage('decrypt',message,key))
def encode():
string = input("请输入字符串编码:\n")
e_string1 = ""
e_string2 = ""
for index in string:
for i in range(0,26):
if index == alphabet:
e_string1 += first_cipher
e_string2 += second_cipher
break
print("第一个编码方法的结果是:\n"+e_string1)
print("第二种编码方法的结果为:\n"+e_string2)
return
def decode():
e_string = input("请输入要解码的字符串:\n")
e_array = re.findall(".{5}",e_string)
d_string1 = ""
d_string2 = ""
for index in e_array:
for i in range(0,26):
if index == first_cipher:
d_string1 += alphabet
if index == second_cipher:
d_string2 += alphabet
print("第一解码方法结果为:\n"+d_string1)
print("第二解码方法结果为:\n"+d_string2)
return