ADFGVX密码

简介: ADFGVX密码

ADFGVX密码

  • 加密对象:小写字母,0-9
  • 原理:
  • 改密码在加密和解密时需要输入两个东西,一个密码表,一个密钥,密码表时6x6的polybius表格,里面的字符是a-z,0-9打乱顺序的,出去j刚好填满36个格子。其中的横纵坐标是由ADFGX表示的,如密码表为:“xyzdefghijklmnopqrstuvwcba0123456789j”,如下:
A D G F V X
A x y z d e f
D g h i k l m
G n o p q r s
F t u v w c b
V a 0 1 2 3 4
X 5 6 7 8 9
  • 将密文的每个字符查表转为相应的ADGFX字符对,前面是横排,后面是纵列,如字符o查表为GD。
  • 再将刚刚查表后的字符串根据密钥如下排列(假设密钥是linux, 刚刚查表后得到的是VGDDXGDFFGFFGG):
l i n u x
V G D D X
G D F F X
F F G G

  • 再根据密钥字符的ascii码大小对上表的列按从小到大排列
i l n u x
V G D D X
D G F F X
F F F G

  • 最后按列将密文去除,如上表取出为:VDFGGFDFFDFGXX
  • 特点:
  • 密文长度是明文的两倍,即密文是偶数
  • 密文字符仅有ADFGVX这几个字符
  • 代码
# write by 2021/7/19
# ADFGVX密码, 是ADFGVX的加强版,可以加密a-z,0-9的字符串
import re
INDEX_DIC = "ADFGVX"
def judge_table(table):
    if len(table) != 36:
        print("密码表出问题", len(table))
        return 0
    return 1
def sort_key(key):
    key_len = len(key)
    key_lis = re.findall(".", key)
    index_lis = [i for i in range(key_len)]
    # print(key_lis)
    # print(index_lis)
    # 冒泡法
    for i in range(key_len):
        for j in range(key_len-i-1):
            if key_lis[j] > key_lis[j+1]:
                key_lis[j], key_lis[j+1] = key_lis[j+1], key_lis[j]
                index_lis[j], index_lis[j+1] = index_lis[j+1], index_lis[j]
    # print(key_lis)
    # print(index_lis)
    return index_lis
def encrypt_adfgvx(string, table, key):
    if not judge_table(table):
        return -1
    ciphertext = ""
    ciphertext_temp = ""
    for i in string:
        index = table.index(i)
        ciphertext_temp += INDEX_DIC[index // 6] + INDEX_DIC[index % 6]
    ciphertext_lis = re.findall(".{1,"+str(len(key))+"}", ciphertext_temp)
    # print("ciphertext_lis", ciphertext_lis)
    sort_index = sort_key(key)
    for index in sort_index:
        for j in ciphertext_lis:
            try:
                ciphertext += j[index]
            except:
                pass
    # print(ciphertext_temp)
    return ciphertext
def decrypt_adfgvx(string, table, key):
    key_len = len(key)
    string_len = len(string)
    if not judge_table(table):
        return -1
    if string_len % 2 != 0:
        return -1
    plaintext = ""
    plaintext_temp = ""
    plaintext_lis = []
    # 一列最小个数
    min_lin = string_len // key_len
    # 有多少最大个数的列
    max_col = string_len % key_len
    # print(f"m={m}, n={n}")
    index = 0
    # 分组
    sort_index = sort_key(key)
    for i in sort_index:
        if i < max_col:
            plaintext_lis.append(string[index:index+min_lin+1])
            index += min_lin+1
        else:
            plaintext_lis.append(string[index:index+min_lin])
            index += min_lin
    # 还原列顺序
    for i in range(key_len-1):
        for j in range(key_len-i-1):
            if sort_index[j] > sort_index[j+1]:
                sort_index[j], sort_index[j+1] = sort_index[j+1], sort_index[j]
                plaintext_lis[j], plaintext_lis[j+1] = plaintext_lis[j+1], plaintext_lis[j]
    # 合成字符串,并转为横纵坐标
    for i in range(min_lin+1):
        for j in plaintext_lis:
            try:
                plaintext_temp += str(INDEX_DIC.index(j[i]))
            except:
                pass
    # 查表
    plaintext_num_lis = re.findall(".{2}", plaintext_temp)
    for i in plaintext_num_lis:
        plaintext += table[int(i[0])*6 + int(i[1])]
    # print(plaintext_temp)
    return plaintext
if __name__ == '__main__':
    table_ = "abcdefghijklmnopqrstuvwxyz0123456789"
    key_ = "china"
    ciphertext_ = encrypt_adfgvx("linux", table_, key_)
    plaintext_ = decrypt_adfgvx(ciphertext_, table_, key_)
    print(f"{plaintext_}: {ciphertext_}")


目录
相关文章
|
3月前
|
数据安全/隐私保护
登录中用于记住用户名和密码的方法
登录中用于记住用户名和密码的方法
30 0
|
5月前
|
安全 数据安全/隐私保护
如何安全的使用密码登录账号(在不知道密码的情况下)
该内容介绍了如何使用一个工具来便捷地复制和管理账号密码。首先提到了两个下载工具的链接,分别是百度网盘和蓝奏云,并给出了相应的提取码。接着,展示了工具的界面,说明通过按住Ctrl或Alt点击密码栏可以快速复制账号和密码,无需直接看到密码。用户可以通过模拟添加账号来体验这一功能,然后演示了如何生成和复制新密码。最后,重点强调了按住Ctrl复制账号和按住Alt复制对应密码的快捷操作,使得在不知密码的情况下也能轻松获取。
|
5月前
|
存储 安全 网络安全
如何取安全的密码?
如何取安全的密码?
43 0
|
5月前
|
存储 弹性计算 运维
循环测试用户名与密码是否正确
【4月更文挑战第29天】
28 0
|
5月前
|
存储 弹性计算 运维
测试用户名与密码是否正确
【4月更文挑战第29天】
34 0
|
数据安全/隐私保护 计算机视觉
QWE密码
QWE密码
339 0
|
Linux 数据安全/隐私保护 知识图谱
ADFGX密码
ADFGX密码
239 0
|
Linux 数据安全/隐私保护
普莱费尔密码(playfair)
普莱费尔密码(playfair)
182 0
|
机器学习/深度学习 Linux 数据安全/隐私保护
维吉尼亚密码(Vigenere)
维吉尼亚密码(Vigenere)
187 0
|
安全 算法 数据安全/隐私保护
C/C++编程题之简单密码
密码是我们生活中非常重要的东东,我们的那么一点不能说的秘密就全靠它了。哇哈哈. 接下来渊子要在密码之上再加一套密码,虽然简单但也安全。