python字母移位_Python实现简单的移位密码 (加解密)

对输入字符进行编码处理

如需修改 移位方向,移位参数,请转至文档末尾

# 编码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值