Ghost689 2022-10-18 14:58 采纳率: 92.6%

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

• 写回答

#### 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'))
``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论 编辑记录

#### 问题事件

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

#### 悬赏问题

• ¥15 evo评估时曲线出现问题
• ¥15 eNSP拓扑图配置实验
• ¥20 有没有兄弟会替换fmod studio的.bank文件内的音效？
• ¥20 如何解决访问网址被屏蔽的情况
• ¥15 本地运行的vue2项目，使用内网穿透，前端界面一直转圈圈
• ¥20 powerbuilder datawindow控件导出Excel数据，可不可以不自动覆盖原数据，而是在后面新插入入数据。
• ¥100 无轴承永磁同步电机控制
• ¥15 eps里添加本地倾斜模型
• ¥15 telegram 问题
• ¥15 nrf52810-c三个a 程序