大家好,欢迎来到IT知识分享网。
棋盘密码(Polybius)
-
加密对象:小写字母
-
原理:
-
1 2 3 4 5 1 a b c d e 2 f g h i,j k 3 l m n o p 4 q r s t u 5 v w x y z - 密文就是字符在密码表里面对应的横纵坐标,如”a”加密为”11″, “y”加密为”54”
棋盘密码是一种查表加密法,密码表如下:
-
-
特点:
- 数字没两个一组
- 数字范围为1~5
-
实例:加密字符串”polybius”:
- 查表替换: “p”对应”35”, “o”对应”34”, “l”对应”31”, “y”对应”54”, “b”对应”12”, “i”对应”24”, “u”对应”45”, “s”对应”43”。
- 故密文为: “35 34 31 54 12 24 45 43”
-
代码
# write by 2021/7/6 # 棋盘密码 # i 和 j 在同一个格子 CHECKERBOARD = "abcdefghiklmnopqrstuvwxyz" def encrypt_polybius(string): ciphertext = "" for i in string.replace("j", "i").replace(" ", ""): if i in CHECKERBOARD: index = CHECKERBOARD.index(i) ciphertext += str(index // 5 + 1) + str(index % 5 + 1) + " " else: return -1 return ciphertext.strip() def decrypt_polybius(string): plaintext = "" lis = string.split(" ") try: for i in lis: index = (int(i[0])-1)*5+int(i[1])-1 plaintext += CHECKERBOARD[index] if index == 9: plaintext += "(j)" except: return -1 return plaintext if __name__ == '__main__': ciphertext_ = encrypt_polybius("polybius") print(ciphertext_) plaintext_ = decrypt_polybius(ciphertext_) print(f"{plaintext_}: {ciphertext_}")
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/140327.html