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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分