Cryptography With Python 简明教程

Implementation of One Time Pad Cipher

Python 包含一个 hacky 实现模块,用于 one-time-pad 密码实现。该软件包名称称为一次性密码本,其中包含一个命令行加密工具,该工具使用类似一次性密码本密码算法的加密机制。

Python includes a hacky implementation module for one-time-pad cipher implementation. The package name is called One-Time-Pad which includes a command line encryption tool that uses encryption mechanism similar to the one-time pad cipher algorithm.

Installation

您可以使用以下命令安装此模块:

You can use the following command to install this module −

pip install onetimepad

如果您想从命令行使用它,请运行以下命令:

If you wish to use it from the command-line, run the following command −

onetimepad
pip

Code

以下代码有助于生成一次性密码本密码:

The following code helps to generate a one-time pad cipher −

import onetimepad

cipher = onetimepad.encrypt('One Time Cipher', 'random')
print("Cipher text is ")
print(cipher)
print("Plain text is ")
msg = onetimepad.decrypt(cipher, 'random')

print(msg)

Output

当您运行上面给出的代码时,您可以观察到以下输出:

You can observe the following output when you run the code given above −

pip output

Note - 如果密钥的长度小于消息(纯文本)的长度,那么加密的消息非常容易破解。

Note − The encrypted message is very easy to crack if the length of the key is less than the length of message (plain text).

在任何情况下,密钥都不一定是随机的,这使得一次性密码本密码成为一种有价值的工具。

In any case, the key is not necessarily random, which makes one-time pad cipher as a worth tool.