python中替换解密的错误

时间:2011-02-27 15:13:14

标签: python encryption substitution


import random
orig=list(range(1,65))
temp_orig= [[0, 1, 2, 3, 4, 5, 6, 7],
    [8, 9, 10, 11, 12, 13, 14, 15],
    [16, 17, 18, 19, 20, 21, 22, 23],
    [24, 25, 26, 27, 28, 29, 30, 31],
    [32, 33, 34, 35, 36, 37, 38, 39],
    [40, 41, 42, 43, 44, 45, 46, 47],
    [48, 49, 50, 51, 52, 53, 54, 55],
    [56, 57, 58, 59, 60, 61, 62, 63]]

ip= [[40, 24, 9, 41, 42, 56, 43, 45], [4, 23, 21, 60, 35, 6, 59, 0], [36, 53, 32, 16, 7, 37, 17, 18], [10, 62, 61, 38, 29, 34, 31, 25], [54, 57, 51, 49, 39, 3, 50, 30], [11, 46, 33, 27, 44, 15, 13, 48], [12, 58, 1, 26, 47, 20, 28, 52], [19, 55, 2, 63, 22, 8, 14, 5]]

ipinv=[[15, 50, 58, 37, 8, 63, 13, 20], [61, 2, 24, 40, 48, 46, 62, 45], [19, 22, 23, 56, 53, 10, 60, 9], [1, 31, 51, 43, 54, 28, 39, 30], [18, 42, 29, 12, 16, 21, 27, 36], [0, 3, 4, 6, 44, 7, 41, 52], [47, 35, 38, 34, 55, 17, 32, 57], [5, 33, 49, 14, 11, 26, 25, 59]]

print "\n\nOriginal position of Bits:" for i in range(len(ipinv)): print "\t",temp_orig[i] print "\n\nInitial Permutation Table" for i in range(len(ipinv)): print "\t",ip[i] print "\n\nInverse Initail permutation Table" for i in range(len(ipinv)): print "\t",ipinv[i]

print "\nRound 1:Initial Permutation"

plaintext=list(raw_input("\n\tEnter not more than 8 char:"))

l=len(plaintext) for i in range(8,len(plaintext)): del plaintext[l-1+8-i]

print "\n\tPlain text:",plaintext,"\n"

for i in range(8): plaintext[i]=list(bin(ord(plaintext[i])).zfill(8)) print "\t",plaintext[i]

ip1=temp_orig for i in range(8): for j in range(8): ip1[i][j]=plaintext[(ip[i][j])/8][(ip[i][j])%8]

print "\nEnciphered list:" for i in range(8): print "\t",ip1[i] print "\n73\n",ip1[7][3],"\n73"

ip1_d=temp_orig

for i in range(8): for j in range(8): ip1_d[i][j]=ip1[(ipinv[i][j])/8][(ipinv[i][j])%8] print ip1[7][3],"ip1_d",[i],[j],"=ip1[",(ipinv[i][j])/8,"][",(ipinv[i][j])%8,"]" #print "ip1_d",[i],[j],"=",ip1[(ipinv[i][j])/8][(ipinv[i][j])%8]

print "\nDeciphered list:" for i in range(8): print "\t",ip1_d[i]

de sunstitution变得错误,例如我尝试打印ip1[7][3]并且突然它在i=7j=3时更改了值而没有任何分配给ip1

1 个答案:

答案 0 :(得分:2)

你做ip1=temp_orig几次。这告诉我你可能认为这会复制temp_orig列表 - 但事实并非如此。您必须在此处明确复制列表:

ip1=[list(row) for row in temp_orig]

等等。您的代码只为temp_orig提供了新名称,但仍然会修改temp_orig列表。

我认为这是我在一段时间内看到的最混乱的代码。您应该开始考虑更改数据的转换,而不是像这样杂乱的索引。我仍然不知道你的代码是做什么的,或者它应该如何工作。