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.
PyCipher is released under the terms of the revised (2–clause) BSD license, available here.
The latest version of PyCipher is available for download here.
To install PyCipher, just rename pycipher-0.2.py to pycipher.py and place it in your Python path.
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:
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'
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
A detailed list of changes among versions is available at the end of pycipher.py.
Comments? Suggestions? Bugs? Feel free to contact me.
My name is Aggelos Orfanakos and I am an undergraduate student at the Computer Science Department of the University of Ioannina, Greece.