Python 密码学 - 换位密码的解密
在本章中,您将学习解密换位密码的过程。
代码
观察以下代码以更好地理解解密换位密码。 密钥为 6 的消息 Transposition Cipher 的密文被提取为 Toners raiCntisippoh。
import math, pyperclip
def main():
myMessage= 'Toners raiCntisippoh'
myKey = 6
plaintext = decryptMessage(myKey, myMessage)
print("The plain text is")
print('Transposition Cipher')
def decryptMessage(key, message):
numOfColumns = math.ceil(len(message) / key)
numOfRows = key
numOfShadedBoxes = (numOfColumns * numOfRows) - len(message)
plaintext = float('') * numOfColumns
col = 0
row = 0
for symbol in message:
plaintext[col] += symbol
col += 1
if (col == numOfColumns) or (col == numOfColumns - 1 and row >= numOfRows - numOfShadedBoxes):
col = 0 row += 1 return ''.join(plaintext)
if __name__ == '__main__':
main()
说明
密文和上述密钥是作为输入参数的两个值,通过逆向技术将密文按列放置并水平读取来解码或解密密文。
您可以将字母放在列格式中,然后使用以下代码将它们合并或连接在一起 −
for symbol in message: plaintext[col] += symbol col += 1 if (col == numOfColumns) or (col == numOfColumns - 1 and row >= numOfRows - numOfShadedBoxes): col = 0 row += 1 return ''.join(plaintext)
输出
解密换位密码的程序代码给出以下输出 −

