该密码和栅栏密码类似,是一种移位密码
秘钥就是行列数,但我感觉只需要列数就好了(可能是我理解有问题),使用行列数构成一个表格,将明文依次填入,例如:行列为6列3行, 明文为: “flag{y0u_are_p1g@}”
f | l | a | g | { | y |
---|---|---|---|---|---|
0 | u | _ | a | r | e |
_ | p | 1 | g | @ | } |
然后从最后一个字符像如下方式串起来即构成了密文:}ey{r@gaga_1pulf0_
# write by 2021/8/4
# 曲路密码
import redef encrypt_bend(string, col, row=10):ciphertext = ""temp = []for i in range(col):temp.append([])for index, i in enumerate(string):temp[index % col].append(i)re_temp = list(reversed(temp))for index, i in enumerate(re_temp):if index % 2 == 0:i = list(reversed(i))ciphertext += "".join(i)return ciphertextdef decrypt_bend(string, col, row=10):plaintext = ""length = len(string)min_row = length // col # 最小的行数min_num = col - length % col # 最小行数的列数# 分组temp = []index = 0for i in range(col):if i < min_num:temp.append(string[index:index+min_row])index += min_rowelse:temp.append(string[index:index+min_row+1])index += min_row + 1print(temp)# 改回列顺序for index, i in enumerate(temp):if index % 2 == 0:# print(re.findall(".{1}", temp[index]))temp[index] = "".join(list(reversed(re.findall(".{1}", temp[index]))))verse()for i in range(length):plaintext += temp[i % col][i // col]return plaintextif __name__ == '__main__':col_ = 7row_ = 5ciphertext_ = encrypt_bend("i will beat you this day", col_, row_)plaintext_ = decrypt_bend(ciphertext_, col_, row_)print(f"{plaintext_} : {ciphertext_}")
本文发布于:2024-02-04 14:29:29,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170709365056377.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |