关键字密码(Nihilist)

简介: 关键字密码(Nihilist)

关键字密码(Nihilist)

  • 加密对象:小写字母
  • 原理:
  • 是棋盘密码的变种,相对于棋盘密码,改密码的密码表是变化的,相对于关键字而变化
  • 关键字就是一
  • 种秘钥,由字母组成,由加密双方约定而成。密码表有25个位置,依次不重复的填入秘钥,在不重复的填入a~z.
  • 如:秘钥为linux,则密码表为:

1
2 3 4 5
1 l i n u x
2 a b c d e
3 f g h k m
4 o p q r s
5 t v w y z

  • 在将明文的每个字符通过查表找出行号和列号,行号在前,列号在后就组成了密文,最后将所有字符的密文以空格隔开组成最后的密文
  • 如:在"linux"为关键字的情况
  • 下,加密"abc"
  • a -> 21
  • b -> 22
  • c -> 23
  • 故密文为: “21 22 23”
  • 代码
# write by 2021/7/6
# 关键字密码,一种棋盘密码的变种
def create_table(key):
    key = key + "abcdefghiklmnopqrstuvwxyz"
    table = ""
    for i in key.replace("j", "i"):
        if i not in table:
            table += i
    return table
def encrypt_nihilist(string, key):
    ciphertext = ""
    table = create_table(key)
    for i in string.replace("j", "i").replace(" ", ""):
        if i in table:
            index = table.index(i)
            ciphertext += str(index // 5 + 1) + str(index % 5 + 1) + " "
        else:
            return -1
    return ciphertext.strip()
def decrypt_nihilist(string, key):
    plaintext = ""
    table = create_table(key)
    lis = string.split(" ")
    try:
        for i in lis:
            index = (int(i[0]) - 1) * 5 + int(i[1]) - 1
            plaintext += table[index]
            if table[index] == "i":
                plaintext += "(j)"
    except:
        return -1
    return plaintext
if __name__ == '__main__':
    ciphertext_ = encrypt_nihilist("linux", "linux")
    plaintext_ = decrypt_nihilist(ciphertext_, "linux")
    print(f"{plaintext_}: {ciphertext_}")
目录
相关文章
|
SQL 编解码 网络安全
|
网络协议 网络安全 数据库
python验证公网ip与内网ip
python验证公网ip与内网ip
288 0
|
机器学习/深度学习 算法 安全
密码学系列之五:MD5、SHA1——一文搞懂哈希函数
密码学系列之五:MD5、SHA1——一文搞懂哈希函数
9623 0
|
Python
Python中遇到奇怪的错误ValueError: bad marshal data
不是代码出的问题,是*.pyc文件被改动了。解决方法,删除所有*.pyc文件再运行
5049 0
|
SQL 关系型数据库 MySQL
go语言数据库中mysql驱动安装
【11月更文挑战第2天】
360 4
|
10月前
|
Shell Linux Ruby
Python3虚拟环境venv
`venv` 是 Python 的虚拟环境工具,用于为不同项目创建独立的运行环境,避免依赖冲突。通过 `python3 -m venv` 命令创建虚拟环境,并使用 `source bin/activate` 激活。激活后,所有 Python 包将安装在该环境中,不影响系统全局环境。退出环境使用 `deactivate` 命令。每个虚拟环境拥有独立的包集合,确保项目间的隔离性。删除虚拟环境只需删除其目录即可。
847 34
|
11月前
|
算法 数据安全/隐私保护 Python
DES加密初探
本文介绍了Python中常用的DES和3DES加解密方法,包括ECB和CBC模式。通过示例代码展示了如何使用`Crypto`和`pyDes`库实现加解密,并讨论了不同的填充方式。最后,通过一道CTF例题,详细解析了从图像中提取密文、进行ASCII转换、Base64解码、凯撒解码和最终的DES解密过程。
426 4
DES加密初探
|
Linux 数据安全/隐私保护 iOS开发
CTF—图像隐写三板斧(续)
CTF—图像隐写三板斧(续)
662 0
|
数据采集 存储 编解码
一份简明的 Base64 原理解析
Base64 编码器的原理,其实很简单,花一点点时间学会它,你就又消除了一个知识盲点。
424 3
|
存储 索引 Python
一文让你搞懂 Python 的 pyc 文件
一文让你搞懂 Python 的 pyc 文件
618 10