如何解密列换位密码

时间:2019-01-14 22:49:32

标签: python encryption cryptography

我的问题不是每个说话的编码之一,而是理解算法。

从概念上讲,我了解列转置如何以恒定的键值来解密文本,例如10。

当键是排列时,我会感到困惑。例如key = [2,4,6,8,10,1,3,5,7,9]和类似"XOV EK HLYR NUCO HEEEWADCRETL CEEOACT KD"的消息。我感到困惑的部分是将密文写入行,然后根据密钥对行进行排列。

有人可以对此提供一些说明吗。

2 个答案:

答案 0 :(得分:0)

我知道了。一旦知道行数和列数,就可以将密文写入行中,然后根据密钥对行进行置换。如果我的解释错误,请更正。纯文本是“您已经破解了代码的出色工作”

答案 1 :(得分:0)

从本质上讲,密钥成为密码中每个x个字母的顺序,x是密钥的长度。在您的示例情况下,密钥的长度为10,因此,请按其对应的订单号排列前10个字母。

这就是解释,这是我编写的一些代码,用于使用密钥以正确的顺序排列密码:

import math

moves = [2,4,6,8,10,1,3,5,7,9]
msg = "XOV EK HLYR NUCO HEEEWADCRETL CEEOACT KD"

decrypted = list(msg)

for i, letter in enumerate(msg):
    moves_index = i % len(moves)

    index = (math.floor(i / len(moves)) * len(moves)) + moves[moves_index]

    decrypted[index - 1] = letter

    print(str.format('{}, at index {}, goes in destination index {} (letter number {})', letter, i, index - 1, index))

print(''.join(decrypted))

这会打印出每个步骤,以便您可以对照手动进行验证:

X, at index 0, goes in destination index 1 (letter number 2)
O, at index 1, goes in destination index 3 (letter number 4)
V, at index 2, goes in destination index 5 (letter number 6)
 , at index 3, goes in destination index 7 (letter number 8)
E, at index 4, goes in destination index 9 (letter number 10)
K, at index 5, goes in destination index 0 (letter number 1)
 , at index 6, goes in destination index 2 (letter number 3)
H, at index 7, goes in destination index 4 (letter number 5)
L, at index 8, goes in destination index 6 (letter number 7)
Y, at index 9, goes in destination index 8 (letter number 9)
R, at index 10, goes in destination index 11 (letter number 12)
 , at index 11, goes in destination index 13 (letter number 14)
N, at index 12, goes in destination index 15 (letter number 16)
U, at index 13, goes in destination index 17 (letter number 18)
C, at index 14, goes in destination index 19 (letter number 20)
O, at index 15, goes in destination index 10 (letter number 11)
 , at index 16, goes in destination index 12 (letter number 13)
H, at index 17, goes in destination index 14 (letter number 15)
E, at index 18, goes in destination index 16 (letter number 17)
E, at index 19, goes in destination index 18 (letter number 19)
E, at index 20, goes in destination index 21 (letter number 22)
W, at index 21, goes in destination index 23 (letter number 24)
A, at index 22, goes in destination index 25 (letter number 26)
D, at index 23, goes in destination index 27 (letter number 28)
C, at index 24, goes in destination index 29 (letter number 30)
R, at index 25, goes in destination index 20 (letter number 21)
E, at index 26, goes in destination index 22 (letter number 23)
T, at index 27, goes in destination index 24 (letter number 25)
L, at index 28, goes in destination index 26 (letter number 27)
 , at index 29, goes in destination index 28 (letter number 29)
C, at index 30, goes in destination index 31 (letter number 32)
E, at index 31, goes in destination index 33 (letter number 34)
E, at index 32, goes in destination index 35 (letter number 36)
O, at index 33, goes in destination index 37 (letter number 38)
A, at index 34, goes in destination index 39 (letter number 40)
C, at index 35, goes in destination index 30 (letter number 31)
T, at index 36, goes in destination index 32 (letter number 33)
 , at index 37, goes in destination index 34 (letter number 35)
K, at index 38, goes in destination index 36 (letter number 37)
D, at index 39, goes in destination index 38 (letter number 39)

但是,最终打印出的结果是:KX OHVL YEOR HNEUECREEWTALD CCCTE EKODA,它与您建议的解决方案不匹配:execlent work you have cracked the code(显然,忽略了大小写)。不确定差异是什么...