日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中字符移位加密_1.1 移位密码加密解密python实现

發布時間:2024/10/8 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中字符移位加密_1.1 移位密码加密解密python实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

總結

以上是生活随笔為你收集整理的python中字符移位加密_1.1 移位密码加密解密python实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。