发布网友 发布时间:2022-04-23 08:50
共3个回答
热心网友 时间:2022-04-18 09:31
修改后运行结果:
a. 加密
1.进行栅栏加密
2.进行栅栏解密
请选择: 1
请输入分组数目 2
请输入列排编号 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)
热心网友 时间:2022-04-18 10:49
#! /usr/bin/env python
#coding=utf-8
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 (0,n):
m=int(col[j])-1
print arr[i][m],
print "\r\n"
print u"1.进行栅栏加密\r\n 2.进行栅栏解密\r\n请选择:".encode("gb2312"),
热心网友 时间:2022-04-18 12:23
答案不对的,我试过了,因为你排序改变很乱时就不行的,修改后的答案,无论排序怎样都可以的。。
#! /usr/bin/env python
#coding=utf-8
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
print u"原明文:".encode("gb2312"),
print "\r"
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"
print "\r"
print u"加密文:".encode("gb2312"),
print "\r"
for i in range(0,row):
for j in range(0,n):
m=int(col[j])-1
print arr[i][m],
print "\r"
def Decrypt(n,col,C):
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
print u"加密文:".encode("gb2312"),
print "\r"
for i in range(0,row):
for j in range(0,n):
if x<len(C):
a=int(col[j])-1
arr[i][a]=C[x]
print C[x],
x+=1
print "\r"
print "\r"
print u"原明文:".encode("gb2312"),
print "\r"
for i in range(0,row):
for j in range(0,n):
print arr[i][j],
print "\r"
print u"1、进行栅栏加密\r\n 2、进行栅栏解密\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)追问这位大哥 能解释下解密么?还是没搞懂~
追答在解密的时候,如果你解密的步骤和加密的一样,岂不是继续加密了吗??所以解密的时候必须逆着加密的步骤解密, a=int(col[j])-1 先把列排序编号赋给a, arr[i][a]=C[x] 把密文分别赋给数组 arr[i][a],这样数组对应的值(即密文)就是加密时明文对应的数组。 for i in range(0,row):
for j in range(0,n): print arr[i][j], 然后(1.2.3……)顺序输出来。