Python密码学

转置密​​码的解密

转置密​​码的解密详细操作教程
在本章中,您将学习解密转置密码的过程。

代码

请观察以下代码,以更好地理解解密转置密码。密钥为 6 的消息 换位密码的密文将作为 Toners raiCntisippoh。
获取。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
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()

说明

密文和提到的密钥是两个值,它们是输入参数,用于通过以列格式放置字符并以水平方式读取它们来以反向技术对密文进行解码或解密。
您可以按列格式放置字母,然后使用以下代码将它们组合或连接在一起-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
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)

输出

用于解密转置密码的程序代码给出以下输出-
解密转置
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4