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

 1 2 3 4 5 1 C I P H E 2 R S A B D 3 F G J K L 4 M N O T U 5 V W X Y Z
 1 2 3 4 5 1 A B C D E 2 F G H I J 3 K L M N O 4 P R S T U 5 V W X Y Z

 1 2 3 4 5 1 A B C D E 2 F G H I J 3 K L M N O 4 P R S T U 5 V W X Y Z
 1 2 3 4 5 1 C R Y P T 2 O A B D E 3 F G H I J 4 K L M N S 5 U V W X Z

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.

 1 2 3 4 5 1 C I P H E 2 R S A B D 3 F G J K L 4 M N O T U 5 V W X Y Z
 1 2 3 4 5 1 A B C D E 2 F G H I J 3 K L M N O 4 P R S T U 5 V W X Y Z

 1 2 3 4 5 1 A B C D E 2 F G H I J 3 K L M N O 4 P R S T U 5 V W X Y Z
 1 2 3 4 5 1 C R Y P T 2 O A B D E 3 F G H I J 4 K L M N S 5 U V W X Z

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.