python实现换位密码算法

一 介绍

换位密码基本原理:先把明文按照固定长度进行分组,然后对每一组的字符进行换位操作,从而实现加密。例如,字符串“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]
 result.append(newItem)
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

    原文作者:维吉尼亚加密问题
    原文地址: https://blog.csdn.net/chengqiuming/article/details/78601103
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞