python实现换位密码算法

来源:互联网 发布:dnf哪里数据芯片多 编辑:程序博客网 时间:2024/05/18 11:35

一 介绍

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