s = list(s)
n = 0
for sw in s:
s[n] = chr(ord(sw)+3)
n = n + 1
sout = ''
for sw2 in s:
局逗 sout = sout + 掘腊扒sw2
print '[加密结果]:',sout
解密的类似,主要用到ord、chr函数。
修改后运行结果:a. 加密
1.进行栅栏加密
2.进行栅栏解密
请选择: 1
请输入列排编号 21
请输明文 abcdefgh
b a
d c
f e
h g
b. 解洞迅敬密
1.进行栅栏加密
2.进行栅栏解密
请选择: 2
请输入分组数目 2
请输入列排编号 21
请输密文 b a d c f e h g
abcdefgh
修改后的代码:
def Encrypt(n,col,M):
row=len(M)/n
if len(M)%n>0:
row+=1
arr=[[""for x in range(0,n)] for y in range(0,row)]
x=0
for i in range(0,row):
for j in range(0,n):
if x<len(M):
arr[i][j]=M[x]
#print M[x],
x+=1
#print "\r\n"
for i in range(0,row):
for j in range(0,n):
m=int(col[j])-1
print arr[i][m],
print "\r\n"
def Decrypt(n, col, C):
C="".join(C.split())
row=len(C)/n
if len(C)%n>0:
row+=1
arr=[[""for x in range(0,n)] for y in range(0,row)]
x=0
for i in range(0,row):
for j in range(0,n):
if x<len(C):
arr[i][j]=C[x]
#print C[x],
x+=1
#print "\r\n"
M=""
for i in range(0,row):
for j in range(0,n):
m=int(col[j])-1
#print arr[i][m],
M+=arr[i][m]
#print "\r\n"
print M
print u"1.进行栅栏加密\r\n2.进行栅栏解密\r\n请选择:".encode("gb2312"),
sel=raw_input()
if int(sel)==1:
print u"纳慎请输入分组数目".encode("gb2312"),
n=int(raw_input())
print u"请输入列排编号".encode("gb2312"),
col=raw_input()
print u"请输明文".encode("gb2312"),
M=raw_input()
while len(M)<n*3:
print u"明文必须是分组数目的3倍".encode("gb2312")
M=raw_input()
Encrypt(n,col,M)
else :
print u"请输入分组数目".encode("gb2312"),
n=int(raw_input())
print u"请输入列排编号".encode("gb2312"),
col=raw_input()
print u"请输密文".encode("gb2312"),
C=raw_input()
while len(C)<n*3:
print u"密文必须是分组数目的3倍".encode("昌前gb2312")
C=raw_input()
Decrypt(n,col,C)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)