Python 转置字母对 scytale 密码

Python transpose pairs of letters scytale cipher

所以我有一些 python 代码可以为文本字符串运行 scytale 密码, 但是我正在尝试转置字母对而不是单个字母。

plaintext = "abcdefghij"

Current Encoding:
a c e g i 
 b d f h j

Desired Encodeing:
ab  ef  ij
  cd  gh

当前结果 = "acegibdfhj"

理想结果 = "abefijcdgh"

脚本:

plaintext = "abcdefghij"
key = 2

ciphertext = [''] * key
for column in range(key):
  pointer = column
  while pointer < len(plaintext):
    ciphertext[column] += plaintext[pointer]
    pointer += key

print(plaintext)
print(''.join(ciphertext))

这个有效

plaintext = "abcdefghij"

c1 = c2 = ""
for i in range(len(plaintext)//2):
    pointer = 4*i
    c1 += plaintext[pointer:pointer+2]
    c2 += plaintext[pointer+2:pointer+4] 

cipher = c1+c2

可能不是最有效的方法,但肯定有效。根据需要添加边界案例。在 O(n)

中运行