例题1.1 移位密码加密解密
1.1.使用穷尽密钥搜索法破译如下利用移位密码加密的密文:
ESPESTCOPIPCNTDPYPPODACZRCLXXTYR.1.1答案:
K=11,明文序列为: the third exercise needs programming.
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 += r_move
while i > 90:
i -= 26
# 对小写字母进行处理
elif 97 <= i <= 122:
i += r_move
while 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='')
# 右移解码
def decode():
r_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 += r_move #<---(右移符号为'+=',左移为'-=')
while i > 90: #<---(对应上方修改)
i -= 26 #(右移:i>90,i -= 26)
# (左移:i<65,i += 26)
# 对小写字母进行处理
# ↓↓↓↓此处同上修改即可
elif 97 <= i <= 122:
i += r_move
while i > 122:
i -= 26
print(chr(i),end='')
# 主程序入口
answer = input(f'请输入所需的操作:编码/E or 解码/D: ')
if answer.upper() == 'E':
encode()
elif answer.upper() =='D':
decode()
else:
print('输入错误!')
# 1.使用穷尽秘钥搜索法破译如下利用移位密码加密的密文
#
# 结果如下:ESPESTCOPIPCNTDPYPPODACZRCLXXTYR
# 请输入所需的操作:编码/E or 解码/D: D
# 请输入解码移位参数(左移): 11
# 请输入需要解码的字符: ESPESTCOPIPCNTDPYPPODACZRCLXXTYR
# THE THIRD EXERCISE NEEDS PROGRAMMING
# 请输入所需的操作:编码/E or 解码/D: D
# 请输入解码移位参数(右移): 15
# 请输入需要解码的字符: ESPESTCOPIPCNTDPYPPODACZRCLXXTYR
# THE THIRD EXERCISE NEEDS PROGRAMMING
版本二:
# 编码
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 += r_move
while i > 90:
i -= 26
# 对小写字母进行处理
elif 97 <= i <= 122:
i += r_move
while i > 122:
i -= 26
print(chr(i), end='')
def encode(s, r_move):
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 += r_move
while i > 90:
i -= 26
# 对小写字母进行处理
elif 97 <= i <= 122:
i += r_move
while i > 122:
i -= 26
i -= 32
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='')
def decode(s, l_move):
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
i += 32
elif 97 <= i <= 122:
i -= l_move
while i < 97:
i += 26
print(chr(i), end='')
if __name__ == '__main__':
answer = input(f'请输入所需的操作:编码/E or 解码/D: ')
i = 1
if answer.upper() == 'E':
s = input('请输入需要加密的字符: ')
r_move = int(input('请输入加密移位参数(右移): '))
encode(s,r_move)
elif answer.upper() == 'D':
s = input('请输入需要解码的字符: ')
while(i <= 25):
decode(s, i)
print('\r')
i = i + 1
else:
print('输入错误!')
# (.venv_pass) PS D:\华师\密码学课程> & d:/华师/密码学课程/.venv_pass/Scripts/python.exe d:/华师/密码
# 学课程/PythonCode/第一次/移位加密与解密.py
# 请输入所需的操作:编码/E or 解码/D: D
# 请输入需要解码的字符: ESPESTCOPIPCNTDPYPPODACZRCLXXTYR.
# the third exercise needs programming.