例題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.