棋盘密码(Polybius)

简介: 棋盘密码(Polybius)

棋盘密码(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_}")


目录
相关文章
|
3月前
要求输出国际象棋棋盘
要求输出国际象棋棋盘。
21 1
|
2月前
|
存储
扫雷(二维数组和函数)
扫雷(二维数组和函数)
|
3月前
|
存储 弹性计算 运维
打印国际象棋棋盘
【4月更文挑战第29天】
28 1
|
3月前
|
存储 算法 编译器
扫雷游戏棋盘的打印,判断输赢,深度分析
扫雷游戏棋盘的打印,判断输赢,深度分析
|
10月前
|
机器学习/深度学习 自然语言处理 算法
趣味算法一棋盘的麦子
趣味算法一棋盘的麦子
|
网络架构
棋盘染色问题
N * M的棋盘 每种颜色的格子数必须相同的 上下左右的格子算相邻 相邻格子染的颜色必须不同 所有格子必须染色 返回至少多少种颜色可以完成任务
184 0
|
Python
【基础入门题014】打印正方形棋盘
【基础入门题014】打印正方形棋盘
60 0
LeetCode 1812. 判断国际象棋棋盘中一个格子的颜色
给你一个坐标 coordinates ,它是一个字符串,表示国际象棋棋盘中一个格子的坐标。下图是国际象棋棋盘示意图。
112 0
蓝桥杯---棋盘放麦子.猜生日
蓝桥杯---棋盘放麦子.猜生日
|
数据安全/隐私保护
070.用栈设置密码
070.用栈设置密码
83 0