Python转置字母对scytale密码

发布于 2025-01-21 07:05:15 字数 536 浏览 2 评论 0原文

因此,我有一些Python代码,可以运行Scytale Cipher的文本字符串, 但是,我试图转置字母对而不是单个字母。

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))

So I have some python code that runs the scytale cipher for a string of text,
However I am trying to transpose pairs of letters rather than single letters.

plaintext = "abcdefghij"

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

Desired Encodeing:
ab  ef  ij
  cd  gh

Current Outcome = "acegibdfhj"

Ideal Outcome = "abefijcdgh"

Script:

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))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断肠人 2025-01-28 07:05:16

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)中运行

This works

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

May not be the most efficient way, but surely works. Add the boundary cases as you want. Runs in O(n)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文