Ghost689 2022-10-18 14:58 采纳率: 92.6%
浏览 89
已结题

信息安全,Cryptography密码学,使用带有关键词的维吉尼密码解密密文

信息安全,Cryptography密码学,使用带有关键词”deceptive”的维吉尼密码解密密文,求明文是什么,具体情况见下图

img

  • 写回答

3条回答 默认 最新

  • 游一游走一走 2022-10-18 15:31
    关注

    WEAREDISCOVEREDSAVEYOURSELF

    import re
    
    
    class VigenereCipher(object):
        """
        This class provides methods for enciphering and
        deciphering text using the Vigenere cipher.
        """
    
        def __init__(self):
    
            self.tabularecta = self.__create_tabula_recta()
    
        def __create_tabula_recta(self):
    
            tabularecta = []
    
            for r in range(0, 26):
    
                offset = 0
                row = []
    
                for column in range(0, 26):
                    row.append(chr(r + 65 + offset))
                    offset += 1
                    if offset > (25 - r):
                        offset = offset - 26
    
                tabularecta.append(row)
    
            return tabularecta
    
        def encipher(self, plaintext, keyword):
    
            """
            The plaintext argument can be any string, but
            only the letters a-z and A-Z will be included
            in the encrypted text.
            """
    
            plaintext = self.__process_plaintext(plaintext)
            keywordrepeated = self.__get_keyword_repeated(keyword, len(plaintext))
            ciphertext = []
    
            for index, letter in enumerate(plaintext):
                plaintextindex = ord(letter.upper()) - 65
                keywordindex = ord(keywordrepeated[index]) - 65
    
                # --------------------#
                # Using tabula recta #
                # --------------------#
                encipheredletter = self.tabularecta[keywordindex][plaintextindex]
    
                # ---------------#
                # Using algebra #
                # ---------------#
                # encipheredletter = chr(((plaintextindex + keywordindex) % 26) + 65)
    
                ciphertext.append(encipheredletter)
    
            return "".join(ciphertext)
    
        def decipher(self, ciphertext, keyword):
    
            """
            Decrypts the ciphetext using the keyword.
            Only the letters a-z and A-Z in the
            original text will be present in the
            decrypted text.
            """
    
            keywordrepeated = self.__get_keyword_repeated(keyword, len(ciphertext))
            decipheredtext = []
    
            for index, letter in enumerate(ciphertext):
                keywordindex = ord(keywordrepeated[index]) - 65
    
                # --------------------#
                # Using tabula recta #
                # --------------------#
                decipheredletter = chr(self.tabularecta[keywordindex].index(letter) + 65)
    
                # ---------------#
                # Using algebra #
                # ---------------#
                # decipheredletter = chr((((ord(letter) - 65) - keywordindex) % 26) + 65)
    
                decipheredtext.append(decipheredletter)
    
            return "".join(decipheredtext)
    
        def __process_plaintext(self, plaintext):
    
            plaintext = plaintext.upper()
            plaintext = re.sub("[^A-Z]", "", plaintext)
    
            return plaintext
    
        def __get_keyword_repeated(self, keyword, length):
    
            keyword = keyword.upper()
            keywordrepeated = []
            keywordlength = len(keyword)
            keywordindex = 0
    
            for i in range(0, length):
                keywordrepeated.append(keyword[keywordindex])
                keywordindex += 1
                if keywordindex > keywordlength - 1:
                    keywordindex = 0
    
            return "".join(keywordrepeated)
    
    
    if __name__ == '__main__':
        vc = VigenereCipher()
        print(vc.decipher('ZICVTWQNGRZGVTWAVZHCQYGLMGJ', 'deceptive'))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月19日
  • 已采纳回答 10月18日
  • 修改了问题 10月18日
  • 赞助了问题酬金20元 10月18日
  • 展开全部

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵