# Cryptography/Other Substitution Cyphers

## Playfair Cipher

Playfair cipher consists of a table of 5x5 letters. The table have all of the 26 english letters. The arrangement and distribution of the letters in the table depends on the chosen key.

## Creating the table

Lets have the word ENCRYPTION as a key.

Start with writing the key word in the table, each letter in a cell. If a letter appears more than once in the key word, it is only included once in the table.

The letters (I) and (J) were combined in a single cell, so we will have 24 cells for the rest of 24 letters.

After writing the key word to the table, fill the remaining cells alphabetically.

So far, you will get this table.

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

Note that the letter (N) from the key word was not repeated and the letters (I) and (J) are in the same cell.

## Encryption

Lets have the following sentence as a plaintext: I will meet you.

1. Start by cutting the plaintext into fixed size units, each containing two letters.
2. While creating units, if you get a unit has the same letter, insert (X) between them and transfer the right letter to the next unit.
3. If you have a single letter in the last unit, add (X).

You will get this:

 ```iw il lm ex et yo ux ```

Now we need to exchange each unit with its corresponding ciphered unit. To do that there is 3 rules:

1. If the two letter in the same column,substitute each letter with the one below. If the letter was the last one in the column take the first letter in the same column (IW-->FC).
2. If the two letter in the same row,substitute each letter with the one in the right. If the letter was the most right letter,take the first letter in the left from the same row(LM-->MQ).
3. otherwise,Intersect the row of the first letter in the unit with the column of the second letter in the same unit, this will give the first letter. And intersect the column of the first letter in the unit with the row of the second letter in the same unit to get the seconde letter(IL-->TM).

The encrypted units will be this:

 ```FC TM MQ RU NP RA VZ ```

The ciphertext will be: FCTMMQRUNPRAVZ .

## Decryption

To decrypt, start by cutting the ciphertext into units. Then use the table to get the plaintext using the same rules. However, there are some differences:

1. If the two letter in the same column,substitute each letter with the one above. If the letter was the first one in the column take the last letter in the same column (FC-->I/Jw).
2. If the two letter in the same row,substitute each letter with the one in the left. If the letter was the most left letter,take the first letter in the right from the same row(MQ-->LM).
3. otherwise,Intersect the row of the first letter in the unit with the column of the second letter in the same unit, this will give the first letter. And intersect the column of the first letter in the unit with the row of the second letter in the same unit to get the seconde letter(TM-->I/JL).