【维吉尼亚密码介绍与Python程序代码实现(一)】

介绍

维吉尼亚密码是一种古典密码算法,也被称为多表密码。

维吉尼亚密码曾多次被发明。该方法最早记录在吉奥万·巴蒂斯塔·贝拉索( Giovan Battista Bellaso)于1553年所著的书《吉奥万·巴蒂斯塔·贝拉索先生的密码》(La cifra del. Sig. Giovan Battista Bellaso)中。然而,后来在19世纪时被误传为是法国外交官布莱斯·德·维吉尼亚(Blaise De Vigenère)所创造,因此现在被称为“维吉尼亚密码”。

在这里插入图片描述

在维吉尼亚密码中,密钥是一个与明文长度相同的字符串,用于对每个明文字符进行加密。加密过程中,将明文字符与密钥字符对应,然后使用凯撒密码的方法进行加密。具体来说,将明文字符与密钥字符相加(按字母表顺序),并对26取模,得到密文字符。解密过程则是将密文字符与密钥字符相减,并对26取模,得到明文字符。
在这里插入图片描述

维吉尼亚密码的优点是可以实现较强的安全性,因为它使用了一个变长的密钥,使得破解难度增加。然而,维吉尼亚密码也有一些缺点,例如当密钥长度较短时,仍然容易受到频率分析等攻击方法的破解。

加密

def vigenere_encrypt(plaintext, key):
    ciphertext = ""
    key_index = 0
    for char in plaintext:
        if char.isalpha():
            # 将明文字符转换为0-25的数字
            plaintext_num = ord(char.upper())
            key_num = ord(key[key_index % len(key)].upper()) - ord('A')
            # 加密字符
            encrypted_num = (plaintext_num + key_num) % 26
            # 将加密后的数字转换为字符
            encrypted_char = chr(encrypted_num + ord('A'))
            ciphertext += encrypted_char
            key_index += 1
        else:
            ciphertext += char
    return ciphertext
    

解密

def vigenere_decrypt(ciphertext, key):
    plaintext = ""
    key_len = len(key)
    for i in range(len(ciphertext)):
        # 计算密钥字符的索引
        key_index = i % key_len
        # 计算解密后的字符
        char = chr((ord(ciphertext[i]) - ord(key[key_index])) % 26 + ord('A'))
        # 将解密后的字符加入到明文中
        plaintext += char
    return plaintext
    

主程序

flag = 'n'
while flag == 'n':
    action = input('''维吉尼亚密码
加密(1)/解密(2): ''')
    print("{:-^50s}".format("Split Line"))
    if action == '1':
        plaintext = input('明文: ')
        key = input('密钥: ')
        ciphertext = vigenere_encrypt(plaintext, key)
        print("密文:", ciphertext)
    elif action == '2':
        ciphertext = input('密文: ')
        key = input('密钥: ')
        plaintext = vigenere_decrypt(ciphertext, key)
        print("明文:", plaintext)
    flag = (input('是否退出(y/n)'))
    print("{:-^50s}".format("Split Line"))
    

在这里插入图片描述

大量文本测试

以马太福音为明文,bible为密钥:

在这里插入图片描述

  • 16
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Python实现维吉尼亚密码加解密的完整示例代码: ``` def encrypt_vigenere(plaintext, keyword): """ 使用维吉尼亚密码加密明文。 """ ciphertext = "" keyword = keyword.upper() keyword_len = len(keyword) for i in range(len(plaintext)): char = plaintext[i] if char.isalpha(): keyword_index = i % keyword_len keyword_char = keyword[keyword_index] shift = ord(keyword_char) - 65 if char.isupper(): ciphertext += chr((ord(char) + shift - 65) % 26 + 65) else: ciphertext += chr((ord(char) + shift - 97) % 26 + 97) else: ciphertext += char return ciphertext def decrypt_vigenere(ciphertext, keyword): """ 使用维吉尼亚密码解密密文。 """ plaintext = "" keyword = keyword.upper() keyword_len = len(keyword) for i in range(len(ciphertext)): char = ciphertext[i] if char.isalpha(): keyword_index = i % keyword_len keyword_char = keyword[keyword_index] shift = ord(keyword_char) - 65 if char.isupper(): plaintext += chr((ord(char) - shift - 65) % 26 + 65) else: plaintext += chr((ord(char) - shift - 97) % 26 + 97) else: plaintext += char return plaintext ``` 该代码包含两个函数:`encrypt_vigenere()`用于加密明文,`decrypt_vigenere()`用于解密密文。两个函数都接受两个参数:明文或密文(plaintext或ciphertext)和关键字(keyword)。 示例用法: ``` plaintext = "HELLO WORLD" keyword = "SECRET" ciphertext = encrypt_vigenere(plaintext, keyword) print(ciphertext) # 输出:"DPLLR XPLRH" decrypted_plaintext = decrypt_vigenere(ciphertext, keyword) print(decrypted_plaintext) # 输出:"HELLO WORLD" ``` 注意:在使用维吉尼亚密码进行加密时,关键字应该是不易猜测的,否则加密的安全性会受到影响。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值