Cryptography With Python 简明教程

Cryptography with Python - Reverse Cipher

上一章概述了如何在本地计算机上安装 Python。在本章中,你将详细了解反向密码及其编码。

The previous chapter gave you an overview of installation of Python on your local computer. In this chapter you will learn in detail about reverse cipher and its coding.

Algorithm of Reverse Cipher

反向密码算法具有以下特征:

The algorithm of reverse cipher holds the following features −

  1. Reverse Cipher uses a pattern of reversing the string of plain text to convert as cipher text.

  2. The process of encryption and decryption is same.

  3. To decrypt cipher text, the user simply needs to reverse the cipher text to get the plain text.

Drawback

  • 反向密码的主要缺点是它非常弱。黑客可以轻松破解密文以获取原始消息。因此,反向密码不被认为是维护安全通信通道的良好选择。

The major drawback of reverse cipher is that it is very weak. A hacker can easily break the cipher text to get the original message. Hence, reverse cipher is not considered as good option to maintain secure communication channel,.

dawback

Example

考虑一个示例,该示例中将使用反向密码算法实现语句 This is program to explain reverse cipher 。以下 Python 代码使用该算法来获取输出。

Consider an example where the statement This is program to explain reverse cipher is to be implemented with reverse cipher algorithm. The following python code uses the algorithm to obtain the output.

message = 'This is program to explain reverse cipher.'
translated = '' #cipher text is stored in this variable
i = len(message) - 1

while i >= 0:
   translated = translated + message[i]
   i = i - 1
print(“The cipher text is : “, translated)

Output

你可以在以下图像中看到反转后的文本,即输出:

You can see the reversed text, that is the output as shown in the following image −

output

Explanation

  1. Plain text is stored in the variable message and the translated variable is used to store the cipher text created.

  2. The length of plain text is calculated using for loop and with help of index number. The characters are stored in cipher text variable translated which is printed in the last line.