Monday, October 28, 2013

Playfair Encryption/Decryption

I'm currently in the middle of a Grad course in Secure Protocols. In the first weeks of the course, we discussed a number of encryption methods used over the years to secure messages passed between entities. These included the Caesar Cipher, the Affine Caesar Cipher, The Hill Cipher, and the Playfair Cipher. Each of these were fairly unique in the way that they were implemented to create an encrypted message from the original plaintext. 

Since most of my time outside of work these days has been finding ways to break encrypted messages (Matasano Crypto Challenges, Codebreaking Puzzles, etc). Our instructor allowed us to do a programming project as part of the Midterm so I figured I would flip things around and write a program that does that encryption and decryption instead. So I wrote one to handle Playfair encryption and decryption. As with the other crypto stuff I've been working on lately, I decided also to write it in Python.


The program would be designed to generate a Playfair matrix, obtain either the plaintext or ciphertext message, and either encrypt or decrypt the message as necessary using the generated matrix. Finally, the program would return the resulting ciphertext in the case where the message was encrypted or the plaintext when the message was decrypted.

The rules of the Playfair Cipher were as follows:
1.     Repeating plaintext letters that are in the same pair are separated with a filler letter, such as x, so that balloon would be treated as ba lx lo on.
2.     Two plaintext letters that fall in the same row of the matrix are each replaced by the letter to the right, with the first element of the row circularly following the last. For example, ar is encrypted as RM.
3.     Two plaintext letters that fall in the same column are each replaced by the letter beneath, with the top element of the column circularly following the last. For example, mu is encrypted as CM.
4.     Otherwise, each plaintext letter in a pair is replaced by the letter that lies in it own row and the column occupied by the other plaintext letter. Thus, hs becomes BP and ea becomes IM (or JM, as the encipherer wishes).

Stallings (2013-03-22). Cryptography and Network Security: Principles and Practice, 6/e (Page 40). Prentice Hall. Kindle Edition.

Using these rules, I created the Playfair Cipher encryption program, the course code for which is included in the Appendix. The letter J was purposely left out of the Playfair matrix generated using the program I developed, as per rule 4 above. All instances of the letter ‘I’ and the letter ‘J’ in the ciphertext are denoted as the letter ‘I’. Otherwise, all rules are followed as described in the text. 


Run-time Program Output:

Message Encryption

$ ./playfair.py


Initial PLAYFAIR matrix:

A B C D E
F G H I K
L M N O P
Q R S T U
V W X Y Z


Enter a key:
this is a test key

Keyed PLAYFAIR matrix:

T H I S A
E K Y B C
D F G L M
N O P Q R
U V W X Z


Would you like to encrypt or decrypt a message?
1 - Encrypt message
2 - Decrypt Message

Decision:
1

Encrypt Message:
Enter the message you would like to encrypt.
The only valid characters are the letters A-Z.
Periods may be denoted with an X
i am encrypting a message here x this is the original message

The message you entered was: IAMENCRYPTINGAMESSAGEHEREXTHISISTHEORIGINALMESSAGE

Your encrypted message is: STDCREPCNITPMIDCBSATDYTKNCUSISASAHTKPNYPTPSMDCBSATDY

Message Decryption:

$ ./playfair.py

Initial PLAYFAIR matrix:

A B C D E
F G H I K
L M N O P
Q R S T U
V W X Y Z


Enter a key:
this is a test key

Keyed PLAYFAIR matrix:

T H I S A
E K Y B C
D F G L M
N O P Q R
U V W X Z


Would you like to encrypt or decrypt a message?
1 - Encrypt message
2 - Decrypt Message

Decision:
2

Decrypt Message:
Enter the message you would like to decrypt.
The only valid characters are the letters A-Z.
STDCREPCNITPMIDCBSATDYTKNCUSISASAHTKPNYPTPSMDCBSATDY

The message you entered was: STDCREPCNITPMIDCBSATDYTKNCUSISASAHTKPNYPTPSMDCBSATDY


Your decrypted message is: IAMENCRYPTINGAMESXSAGEHEREXTHISISTHEORIGINALMESXSAGE

The overall program was pretty small, only taking up 275 lines including all of the comments, examples, and documentation. In all, it was a fun experiment and I ]'ll be looking into some more of these as the class continues.

Source code can be found here:

No comments:

Post a Comment