Joel Verhagen

a computer programming blog

Python Playfair Implementation

I've been playing around with the Playfair cipher recently, so I thought it would be a fun little project to write a Python class that can encrypt and decrypt according to the Playfair cipher, so that's exactly what I did.

If you haven't heard of the Playfair cipher, check out my other post about how it works.

As I said before, the class is written Python. I wrote it in Python 2.6 on a Windows 7 machine.

I have a GitHub repository set up for the project.

The current version of this class is version 1.0. Features include:

  • You can set the password you would like to encrypt and decrypt with.
  • You can set the omission rule used to generate the 5 by 5 matrix. Current omission rules are:
    • Replace J with I
    • Omit Q
    • Replace I with J
  • Set what character you would like to use to pad double letter digraphs and odd length inputs.

Here is an example script to demonstrate how easy it is to use...

# import the Playfair library
import playfair

# create a Playfair object for encrypting and decrypting
ph = playfair.Playfair()

# set the password for upcoming encryptions or decryptions
ph.setPassword('Caput Draconis')

# encrypt a plain text phrase and print it out
print ph.encrypt('Emma Watson is the most talented actress.')

.. and the output.

FLKUARCGNIDGCQFLDEPZCPXLIUSOPAAIFBEV