本文主要是介绍曲路密码(Bend crypto),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
曲路密码(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 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]))))temp.reverse()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_}")
这篇关于曲路密码(Bend crypto)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!