Four-Square Cipher

Introduction

The four-square cipher is a substitution cipher. It differs from the other substitution ciphers listed here because it is polygraphic. More precisely, the Four-Square cipher is digraphic. Instead of making substitutions one letter at a time, we will be making substitutions with digraphs or pairs of letters.

We begin the process of enciphering by contructing two Polybius squares. Normally these squares would be filled using two keywords, one for each of the squares. Here we have used the keywords, CIPHERS and CRYPTOS and laid the squares out next to the alphabet squares we were using. The letter 'Q' has been dropped from our alphabet.

 12345
1CIPHE
2RSABD
3FGJKL
4MNOTU
5VWXYZ
 12345
1ABCDE
2FGHIJ
3KLMNO
4PRSTU
5VWXYZ

 12345
1ABCDE
2FGHIJ
3KLMNO
4PRSTU
5VWXYZ
 12345
1CRYPT
2OABDE
3FGHIJ
4KLMNS
5UVWXZ

Our plain text message is 'The secret is out'. To start to encipher this we need to split our plain text up into digraphs,

TH ES EC RE TI SO UT

If our plain text had an odd number of letter characters, we would have padded out with another character.

To encrypt our message we process each digraph. The following diagram shows how the first digraph TH is encrypted as OD.

 12345
1CIPHE
2RSABD
3FGJKL
4MNOTU
5VWXYZ
 12345
1ABCDE
2FGHIJ
3KLMNO
4PRSTU
5VWXYZ

 12345
1ABCDE
2FGHIJ
3KLMNO
4PRSTU
5VWXYZ
 12345
1CRYPT
2OABDE
3FGHIJ
4KLMNS
5UVWXZ

The first letter of the digraph is found in the top left grid. The second letter is found in the top right grid. The yellow squares are the letters from the plain text. The pink squares are the corners of a rectangle formed by the 4 squares. The letter in the top right is the first letter of the cipher text digraph, the letter in the bottom left is the second letter in the cipher text digraph.

Continuing this way encrypts the remainder of the message as,

TH ES EC RE TI SO UT
OD PS PT UR TD UH TS

Programming The Cipher

As with many of the grid based ciphers, we will be using a string to store our 'grids' and using our noggin to map the positions of characters into grid co-ordinates when we need to work with them. The trick to this algorithm is how to use the co-ordinates of the letters.

No programming logic is included for the missing letter. That isn't too tricky for you to add. The message has also been converted to upper case and had all non-letter characters removed.

Encryption

plainAlphabet ← "ABCDEFGHIJKLMNOPRSTUVWXYZ"
cipherAlphabet1 ← "CIPHERSABDFGJKLMNOTUVWXYZ"
cipherAlphabet2 ← "CRYPTOABDEFGHIJKLMNSUVWXZ"
plain ← "THESECRETISOUT"
cipher ← ""
FOR i ← 0 TO plain.LENGTH - 1 STEP 2
   position ← plainAlphabet.IndexOf(plain(i))
   r ← position DIV 5 + 1
   c ← position MOD 5 + 1
   position ← plainAlphabet.IndexOf(plain(i + 1))
   rr ← position \ 5 + 1
   cc ← position Mod 5 + 1
   cipher ← cipher + cipherAlphabet1(5 * r + cc - 6) + cipherAlphabet2(5 * rr + c - 6)
END FOR
OUTPUT cipher

Decryption

plainAlphabet ← "ABCDEFGHIJKLMNOPRSTUVWXYZ"
cipherAlphabet1 ← "CIPHERSABDFGJKLMNOTUVWXYZ"
cipherAlphabet2 ← "CRYPTOABDEFGHIJKLMNSUVWXZ"
plain ← ""
cipher ← "ODPSPTURTDUHTS"
FOR i ← 0 TO cipher.LENGTH - 1 STEP 2
   position ← cipherAlphabet1.IndexOf(cipher(i))
   r ← position DIV 5 + 1
   c ← position MOD 5 + 1
   position ← cipherAlphabet2.IndexOf(cipher(i + 1))
   rr ← position \ 5 + 1
   cc ← position Mod 5 + 1
   plain ← plain + plainAlphabet(5 * r + c - 6) + plainAlphabet(5 * rr + cc - 6)
END FOR
OUTPUT plain

Challenges

A nice set of tools for filling the squares would be a nice addition to any implementation of this cipher. There is also a need to deal with the 'rounding' issue that results from the letter missing from our Polybius square.

An interesting way to implement this cipher would be to animate the encryption process. That's quite a nice project in itself.