Implementation of One Time Pad Cipher



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 −

(myenv) D:\Projects\python\myenv>pip3 install onetimepad
Collecting onetimepad
  Downloading onetimepad-1.4.tar.gz (3.3 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: onetimepad
  Building wheel for onetimepad (pyproject.toml) ... done
  Created wheel for onetimepad: filename=onetimepad-1.4-py3-none-any.whl size=3984 sha256=86d0c217afdd0c8acc51e7334da80255d7ebdb31476dc3bf0288f81b42150730
  Stored in directory: c:\users\mahes\appdata\local\pip\cache\wheels\c0\07\a8\67ac97824598d99846b28c5c06965b48e505ce73d609eb67f2
Successfully built onetimepad
Installing collected packages: onetimepad
Successfully installed onetimepad-1.4

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

(myenv) D:\Projects\python\myenv>onetimepad
Message: Hello World
Key: abcdefghijk
Cipher: 29070f080a4630071b060f

Example - Generating One-Time Pad Cipher

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

main.py

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 −

Cipher text is
3d0f0b443b041f044e27061d1a041c
Plain text is
One Time Cipher

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.

Advertisements