当前位置: 首页 > 工具软件 > AutoKey > 使用案例 >

自动秘钥密码(Autokey)

赵英资
2023-12-01

自动秘钥密码(Autokey)

  • 加密对象: 字母

  • 原理:

    • 该密码玉维吉尼亚密码很像, 不同的是秘钥的构成方式不同。 维吉尼亚密码密码的秘钥是有关键字的重复构成的,而该密码的秘钥是由关键字和明文本身构成,即前面是关键字,后面是明文。(秘钥=关键字+明文)

    • 查表:取第一个明文字符作为横标,取第一个秘钥(上一步构成的秘钥)字符作为纵标,查找对应字符,在去第二个明文字符作为横标,取第二个秘钥字符作为纵标,查找对应字符,…。最后将所有字符合在一起就构成了密文

      ABCDEFGHIJKLMNOPQRSTUVWXYZ
      AABCDEFGHIJKLMNOPQRSTUVWXYZ
      BBCDEFGHIJKLMNOPQRSTUVWXYZA
      CCDEFGHIJKLMNOPQRSTUVWXYZAB
      DDEFGHIJKLMNOPQRSTUVWXYZABC
      EEFGHIJKLMNOPQRSTUVWXYZABCD
      FFGHIJKLMNOPQRSTUVWXYZABCDE
      GGHIJKLMNOPQRSTUVWXYZABCDEF
      HHIJKLMNOPQRSTUVWXYZABCDEFG
      IIJKLMNOPQRSTUVWXYZABCDEFGH
      JJKLMNOPQRSTUVWXYZABCDEFGHI
      KKLMNOPQRSTUVWXYZABCDEFGHIJ
      LLMNOPQRSTUVWXYZABCDEFGHIJK
      MMNOPQRSTUVWXYZABCDEFGHIJKL
      NNOPQRSTUVWXYZABCDEFGHIJKLM
      OOPQRSTUVWXYZABCDEFGHIJKLMN
      PPQRSTUVWXYZABCDEFGHIJKLMNO
      QQRSTUVWXYZABCDEFGHIJKLMNOP
      RRSTUVWXYZABCDEFGHIJKLMNOPQ
      SSTUVWXYZABCDEFGHIJKLMNOPQR
      TTUVWXYZABCDEFGHIJKLMNOPQRS
      UUVWXYZABCDEFGHIJKLMNOPQRST
      VVWXYZABCDEFGHIJKLMNOPQRSTU
      WWXYZABCDEFGHIJKLMNOPQRSTUV
      XXYZABCDEFGHIJKLMNOPQRSTUVW
      YYZABCDEFGHIJKLMNOPQRSTUVWX
      ZZABCDEFGHIJKLMNOPQRSTUVWXY
  • 代码

    # write in 2021/7/13
    # 自动秘钥密码
    
    DIC = "abcdefghijklmnopqrstuvwxyz"
    
    
    def encrypt_autokey(string, key):
        ciphertext = ""
        key += string
        key_index = 0
        for i in string:
            if i not in DIC:
                ciphertext += i
            ciphertext += DIC[(DIC.index(i)+DIC.index(key[key_index])) % 26]
            key_index += 1
        return ciphertext
    
    
    def decrypt_autokey(string, key):
        plaintext = ""
        key_index = 0
        for i in string:
            if i not in DIC:
                plaintext += i
            char = DIC[(DIC.index(i) - DIC.index(key[key_index])) % 26]
            key += char
            plaintext += char
            key_index += 1
        return plaintext
    
    
    if __name__ == '__main__':
        key_ = "linux"
        ciphertext_ = encrypt_autokey("autokey", key_)
        plaintext_ = decrypt_autokey(ciphertext_, key_)
        print(f"{plaintext_}: {ciphertext_}")
    
 类似资料: