python實現換位密碼演算法

2021-08-11 03:51:00 字數 842 閱讀 3849

一 介紹

換位密碼基本原理:先把明文按照固定長度進行分組,然後對每一組的字元進行換位操作,從而實現加密。例如,字串「error should never pass silently」,使用秘鑰1432進行加密時,首先將字串分成若干長度為4的分組,然後對每個分組的字元進行換位,第1個和第3個字元位置不變,把第2個字元和第4個字元交換位置,得到「eorrrs shluoden v repssa liseltny」

二 **

def encrypt(plaintext,t):

result =

length = len(t)

temp =[plaintext[i:i+length]for i in range(0,len(plaintext),length)]

for item in temp[:-1]:

newitem=''

for i in t:

newitem = newitem + item[i-1]

return''.join(result)+ temp[-1]

p ="error should never pass silently"

c = encrypt(p,(1,4,3,2))

print(c)

print(encrypt(c,(1,4,3,2)))

三 執行結果

eorrrhs odlu venep ra ssselintly

error should never pass silently

Python密碼學 檢測英文及破譯換位加密

在破譯密碼時可能會有上千個可能的密匙,計算機會因此給出各種各樣的結果。那麼怎麼找出唯一明文呢?如果讓計算機先判斷結果中是亂碼還是英文篩選一遍,再將可能的結果交給我們,會不會比人為翻查上千個結果好很多呢?判斷英文的方法 我們可以製作乙個包含所有英文單詞的文字檔案 字典檔案 通過計算機將解密後的字串分割...

古典密碼 python實現凱撒密碼

def secret str input 請輸入明文 k int input 請輸入移位 str list list str i 0 for i in range len str list if 96 ord str list i 123 str list i chr ord str list i ...

python 實現演算法 Python實現演算法 一

1.二分查詢 def binary search mylist,item low 0 high len mylist 1 while low high mid low high 2 如果 low high 2不是偶數,python自動將mid向下圓整。guess mylist mid if gues...