Four-square cipher uses four squares, and where we take pairs of letters and, using the first and last square, we find the letters that are bounded by the two letters (like Playfair). It was invented by Felix Delastelle and published in 1902.
Four Square |
Method
It uses four 5x5 matrices arranged in a square. Each matrices contains 25 letters. The upper-left and lower-right matrices are the "plaintext squares" and each contain a standard alphabet. The upper-right and lower-left squares are the "ciphertext squares" and have a mixture of characters.
First we break the message into bigrams, such as ATTACK AT DAWN gives:
AT TA CK AT DA WN
We now uses the four 'squares' and locate the bigram to encrypt in the plain alphabet squares. With 'AT', we take the first letter from the top left square, the the second letter from the bottom right square:
a b c d e Z G P T F f g h i k O I H M U l m n o p W D R C N q r s t u Y K E Q A v w x y z X V S B L M F N B D a b c d e C R H S A f g h i k X Y O G V l m n o p I T U E W q r s t u L Q Z K P v w x y z
Now, like Playfair, determine the the characters in the ciphertext around the corners of the rectangle for 'AT' and this makes:
a b c d e Z G P T F f g h i k O I H M U l m n o p W D R C N q r s t u Y K E Q A v w x y z X V S B L M F N B D a b c d e C R H S A f g h i k X Y O G V l m n o p I T U E W q r s t u L Q Z K P v w x y z
And so we pick off 'TI'
The result becomes:
ATTACKATDAWN TIYBFHTIZBSY
Code
The following is an outline of the code which would implement this:
import sys from pycipher import Foursquare four1='ZGPTFOIHMUWDRCNYKEQAXVSBL' four2='MFNBDCRHSAXYOGVITUEWLQZKP' phrase='ATTACK AT DAWN' if (len(sys.argv)>1): four1=str(sys.argv[1]) if (len(sys.argv)>2): four2=str(sys.argv[2]) if (len(sys.argv)>3): phrase=str(sys.argv[3]) from pycipher import Foursquare s = Foursquare(four1,four2) res=Foursquare(key1=four1,key2=four2).encipher(phrase) print "Cipher: ",res print "Decipher: ",Foursquare(key1=four1,key2=four2).decipher(res)