pycipher is a Python module that implements several well-known classical cipher algorithms: Atbash, Autokey, Beaufort, Caesar, Vernam (a.k.a. one-time pad), and Vigenere.


Using pycipher is pretty straightforward. An example session from the Python interactive interpreter follows:

>>> 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.'

Two things to note here:

  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, since 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')
>>> Vigenere(ciphertext).decipher('kInG') ==\
... Vigenere(ciphertext).decipher('KIng') ==\
... Vigenere(ciphertext).decipher('kiNG')

Both the encipher() and decipher() methods return a cipher object of the same type of the one they belong to. This makes chaining 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 built-in str class, any cipher object can be treated as a string. For instance:

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


pycipher is licensed under the terms of the Simplified BSD License.


From here.

Also available as a Gentoo Linux package: dev-python/pycipher


Aggelos Orfanakos,