Cryptography With Python 简明教程

Encryption of Transposition Cipher

在上一章中,我们学习了移位密码。在本章中,让我们讨论其加密。

In the previous chapter, we have learnt about Transposition Cipher. In this chapter, let us discuss its encryption.

Pyperclip

pyperclip 插件在 Python 编程语言中的主要用法是执行跨平台模块以将文本复制并粘贴到剪贴板中。你可以使用以下命令安装 Python pyperclip 模块

The main usage of pyperclip plugin in Python programming language is to perform cross platform module for copying and pasting text to the clipboard. You can install python pyperclip module using the command as shown

pip install pyperclip

如果系统中已存在该需求,则你可以看到以下输出−

If the requirement already exists in the system, you can see the following output −

pyperclip

Code

以下是使用 pyperclip 作为主模块的 Python 置换密码加密代码:

The python code for encrypting transposition cipher in which pyperclip is the main module is as shown below −

import pyperclip
def main():
   myMessage = 'Transposition Cipher'
   myKey = 10
   ciphertext = encryptMessage(myKey, myMessage)

   print("Cipher Text is")
   print(ciphertext + '|')
   pyperclip.copy(ciphertext)

def encryptMessage(key, message):
   ciphertext = [''] * key

   for col in range(key):
      position = col
      while position < len(message):
         ciphertext[col] += message[position]
			position += key
      return ''.join(ciphertext) #Cipher text
if __name__ == '__main__':
   main()

Output

pyperclip 作为主模块的置换密码加密程序代码给出以下输出:

The program code for encrypting transposition cipher in which pyperclip is the main module gives the following output −

encrypting transposition

Explanation

  1. The function main() calls the encryptMessage() which includes the procedure for splitting the characters using len function and iterating them in a columnar format.

  2. The main function is initialized at the end to get the appropriate output.