PyCipher

Introduction

PyCipher is a Python module that implements several well–known classical cipher algorithms; namely: Atbash, Autokey, Beaufort, Caesar, Vernam (a.k.a. one–time pad), and Vigenère.

License

PyCipher is released under the terms of the revised (2–clause) BSD license, available here.

Download

The latest version of PyCipher is available for download here.

Installation

To install PyCipher, just rename pycipher-0.2.py to pycipher.py and place it in your Python path.

Usage

Using PyCipher is pretty straightforward. What follows is an example session from the Python interactive interpreter.

Encipher and decipher the message “Attack at dawn.” with “King” as the key using the Vigenère cipher:

>>> from pycipher import *
>>> plaintext = 'Attack at dawn.'
>>> key = 'King'
>>> ciphertext = Vigenere(plaintext).encipher(key)
>>> ciphertext
'Kbggms nz nijt.'
>>> Vigenere(ciphertext).decipher(key)
'Attack at dawn.'

Note:

  1. Non–alphabetic input (e.g. “ ” and “.” above) is left as–is.
  2. The input (plaintext/ciphertext) case is preserved through output (ciphertext/plaintext).

The case of the key doesn’t matter, as the case of each of its character is made to conform with the corresponding character in the input (plaintext/ciphertext).

>>> Vigenere(plaintext).encipher('king') ==\
... Vigenere(plaintext).encipher('KING') ==\
... Vigenere(plaintext).encipher('KiNg')
True
>>> Vigenere(ciphertext).decipher('kInG') ==\
... Vigenere(ciphertext).decipher('KIng') ==\
... Vigenere(ciphertext).decipher('kiNG')
True

Both the encipher() and decipher() methods return a cipher object of the same type of the one they belong to. This makes things like the following possible:

>>> Vigenere(plaintext).encipher(key).decipher(key)
'Attack at dawn.'
>>> Caesar(plaintext).encipher(3).decipher(2).decipher(1)
'Attack at dawn.'

Since each cipher is a subclass of the str built–in class, any cipher object can be treated as a string. For instance:

>>> Vigenere(plaintext)[:-1].replace(' ', '').lower()
'attackatdawn'

Documentation

The (pydoc–generated) documentation of PyCipher is available here. Alternatively, once you have installed PyCipher, you can issue the following command in your shell:

$ pydoc /absolute/path/to/pycipher.py

History

A detailed list of changes among versions is available at the end of pycipher.py.

Feedback

Comments? Suggestions? Bugs? Feel free to contact me.

References

Acknowledgements

Project kindly hosted by SourceForge.net.

About

My name is Aggelos Orfanakos and I am an undergraduate student at the Computer Science Department of the University of Ioannina, Greece.