曲路密码(Bend crypto)
- 该密码和栅栏密码类似,是一种移位密码
- 秘钥就是行列数,但我感觉只需要列数就好了(可能是我理解有问题),使用行列数构成一个表格,将明文依次填入,例如:行列为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 re def 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 ciphertext def decrypt_bend(string, col, row=10): plaintext = "" length = len(string) min_row = length // col # 最小的行数 min_num = col - length % col # 最小行数的列数 # 分组 temp = [] index = 0 for i in range(col): if i < min_num: temp.append(string[index:index+min_row]) index += min_row else: temp.append(string[index:index+min_row+1]) index += min_row + 1 print(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])))) temp.reverse() for i in range(length): plaintext += temp[i % col][i // col] return plaintext if __name__ == '__main__': col_ = 7 row_ = 5 ciphertext_ = encrypt_bend("i will beat you this day", col_, row_) plaintext_ = decrypt_bend(ciphertext_, col_, row_) print(f"{plaintext_} : {ciphertext_}")