对输入字符进行编码处理
如需修改 移位方向,移位参数,请转至文档末尾
# 编码
def encode():
list_s = [] r_move = int(input('请输入加密移位参数(右移): '))
s = input('请输入需要加密的字符: ')
for i in s:
list_s.append(ord(i))
for i in list_s:
# 处理空格
if i == 32:
print(' ', end='')
# 对大写字母进行处理
elif 65 <= i 90:
i -= 26
# 对小写字母进行处理
elif 97 <= i 122:
i -= 26
print(chr(i), end='')
对输入字符进行解码处理
# 解码
def decode():
l_move = int(input('请输入解码移位参数(左移): '))
s = input('请输入需要解码的字符: ')
list_s = [] for i in s:
list_s.append(ord(i))
for i in list_s:
if i == 32:
print(' ',end='')
elif 65 <= i <= 90:
i -= l_move
while i < 65:
i += 26
elif 97 <= i <= 122:
i -= l_move
while i < 97:
i += 26
print(chr(i),end='')
主程序入口
answer = input(f'请输入所需的操作:编码/E or 解码/D: ')
if answer.upper() == 'E':
encode()
elif answer.upper() =='D':
decode()
else:
print('输入错误!')
实现效果如下
D:\腾讯手游助手\python\python.exe D:/project/cryptography/Move_Cryot.py
请输入所需的操作:编码/E or 解码/D: d
请输入解码移位参数(左移): 3
请输入需要解码的字符: abc ABC xyz XYZ
xyz XYZ uvw UVW
Process finished with exit code 0
移位方向的修改如下(以编码为例):
# 对大写字母进行处理
elif 65 <= i <= 90:
i += r_move 90: 90,i -= 26)
(左移:i<65,i += 26)
# 对小写字母进行处理
↓↓↓↓此处同上修改即可
elif 97 <= i 122:
i -= 26
作者:SFS_Ccjm