## Try the Four-Square Cipher

Utilize the provided tools below to either encrypt or decrypt a message:

### Encrypt

**Result:**

### Decrypt

**Result:**

### Generated Matrices

### Wonderful moments in history

## The Four-Square Cipher: Historical Background and Encryption Principles

The Four-Square Cipher is a symmetric encryption method invented by Frenchman Felix Delastelle (1840–1902) and is part of modern cryptography. This encryption method encrypts letters in pairs using four 5×5 matrices, each containing 25 letters. Typically, the letter Q is omitted, or I and J are considered the same letter. Alternatively, the method can be adapted to use 6×6 matrices to include 10 digits.

During the encryption process, two English words are chosen as keys, such as "example" and "keyword." After removing duplicate letters, the remaining letters are placed in order into the matrices. These two matrices are placed in the upper right and lower left corners, while the upper left and lower right corners use the sequence A to Z to form the matrices, creating the Four-Square Cipher matrix.

To encrypt a message, letters are separated into pairs, such as "hello world" becoming "he ll ow or ld." Based on the positions in the matrices, the corresponding encrypted letters are found. For example, the encryption process for "he" involves finding the position of "h" in the upper left matrix and "e" in the lower right matrix. Then, in the upper right matrix, the letter in the same row as "h" and the same column as "e" is located, and in the lower left matrix, the letter in the same column as "h" and the same row as "e" is found. These two letters form the encrypted ciphertext.

The weakness of the Four-Square Cipher lies in the case where two letters are in the same column, the original letters are used, which may expose about 20% of the content. Additionally, since it is a symmetric encryption method, it is easily broken through recorded comparisons, resulting in low security. Another issue is that it can only encrypt an even number of characters. If there is an odd number, the last remaining character cannot be encrypted, and using the original character or padding encryption makes the cipher text more vulnerable to exposure.

As part of classical cryptography, although the Four-Square Cipher does not match the security of modern encryption algorithms, its simplicity and historical significance have provided inspiration for modern encryption methods.

## Four-Square Cipher Encryption Step-by-step Guide

The following is a detailed step-by-step guide to encrypting using the** Four-square
Cipher**, with the keywords "Four" and "Cipher" and plaintext "Hello World".

### Step 1: Preparation

**Choose Keywords**- key1 = "Four"
- key2 = "Cipher"

**Prepare Plaintext**- Plaintext is "Hello World".

**Convert Plaintext to Suitable Format for Encryption**- Convert to uppercase letters and replace letter J with I: Hello World -> HELLOWORLD
- Remove non-alphabet characters: HELLOWORLD
- If the plaintext length is odd, pad with the
**character X**at the end.

### Step 2: Generate Letter Squares

Use the keywords to generate four 5x5 letter squares. The alphabet typically omits the ** letter
J**, using** I **instead.

#### Function to Generate Letter Squares

` ````
function createSquare(keyword) {
let alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
let used = {};
let square = "";
keyword = keyword.toUpperCase().replace(/J/g, 'I');
for (let i = 0; i < keyword.length; i++) {
if (!used[keyword[i]]) {
(used[keyword[i]]) = true;
square += keyword[i];
}
}
for (let i = 0; i < alphabet.length; i++) {
if (!used[alphabet[i]]) {
square += alphabet[i];
}
}
return square;
}
```

#### Generate 4 Letter Squares

Square 1 (square1) and Square 4 (square4) using key1 = "FOUR":

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

Square 2 (square2) and Square 3 (square3) using key2 = "CIPHER":

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

### Step 3: Encryption Process

Divide the plaintext into pairs of letters and encrypt each pair. Example plaintext: HELLOWORLD.

#### Divide Plaintext

HELLOWORLD -> HE LL OW OR LD

#### Encrypt Each Pair of Letters

Use the following function to find the position of letters in the squares:

` ````
function findPosition(square, char) {
let index = square.indexOf(char);
let row = Math.floor(index / 5);
let col = index % 5;
return [row, col];
}
```

Encrypt each pair of letters step-by-step:

**Encrypt Pair "HE"**- H is at position (2, 0) in square1
- E is at position (1, 3) in square4
- Take (2, 3) from square2 -> M
- Take (1, 0) from square3 -> R
- Result: HE -> MR

**Encrypt Pair "LL"**- L is at position (2, 3) in square1
- L is at position (2, 3) in square4
- Take (2, 3) from square2 -> M
- Take (2, 3) from square3 -> M
- Result: LL -> MM

**Encrypt Pair "OW"**- O is at position (0, 1) in square1
- W is at position (4, 1) in square4
- Take (0, 1) from square2 -> I
- Take (4, 1) from square3 -> W
- Result: OW -> IW

**Encrypt Pair "OR"**- O is at position (0, 1) in square1
- R is at position (0, 3) in square4
- Take (0, 3) from square2 -> H
- Take (0, 1) from square3 -> I
- Result: OR -> HI

**Encrypt Pair "LD"**- L is at position (2, 3) in square1
- D is at position (1, 2) in square4
- Take (2, 2) from square2 -> L
- Take (1, 3) from square3 -> D
- Result: LD -> LD

### Final Ciphertext

Combine the encrypted pairs of letters. The ciphertext for HELLOWORLD is: **MRMMIWHILD**

## Comparison of Four-Square Cipher, Double Playfair Cipher, and Playfair Cipher

### Similarities

**Polygraphic Substitution Ciphers**- Four-Square Cipher, Double Playfair cipher, and Playfair cipher are all polygraphic substitution ciphers. This means they use pairs or groups of letters as the basic unit for encryption and decryption, rather than single letters.

**Letter Matrices**- All three cipher systems use letter matrices for encryption and decryption. Each method relies on the positions of letters in these matrices to perform substitutions or transformations.

**Objective**- The design goal of these ciphers is to enhance the security of traditional single-letter substitution ciphers by increasing the complexity of encryption, thus preventing simple frequency analysis attacks.

### Differences

#### Four-square Cipher

**Letter Matrices:**Uses four 5x5 letter matrices.**Encryption Unit:**Processes two letters at a time.**Steps:**- Four letter matrices: Two matrices are generated from keywords, and the other two are usually in standard alphabetical order.
- Divide the plaintext into pairs of letters.
- Find the position of each pair of letters in two different matrices, then find the corresponding encrypted letters in the other two matrices.

**Complexity:**Increases complexity and security by using more matrices compared to the Playfair cipher.

#### Double Playfair Cipher

**Letter Matrices:**Uses two 5x5 letter matrices.**Encryption Unit:**Processes two letters at a time.**Steps:**- Two letter matrices are generated from different keywords.
- Divide the plaintext into pairs of letters.
- Use the first matrix to find the position of the first letter, and the second matrix to find the position of the second letter, then substitute the letters according to the rules.

**Complexity:**Adds complexity and security by introducing a second matrix compared to the single Playfair cipher.

#### Playfair Cipher

**Letter Matrices:**Uses one 5x5 letter matrix.**Encryption Unit:**Processes two letters at a time.**Steps:**- One letter matrix is generated from a keyword.
- Divide the plaintext into pairs of letters.
- Replace the letters based on their positional relationship (same row, same column, or different rows and columns).

**Complexity:**Increases complexity compared to simple single-letter substitution ciphers, but is less complex than Double Playfair and Four-Square Ciphers.

### Comparison Table

Feature | Four-Square Cipher | Double Playfair Cipher | Playfair Cipher |
---|---|---|---|

Number of Letter Matrices |
4 | 2 | 1 |

Encryption Unit |
Two letters at a time | Two letters at a time | Two letters at a time |

Keywords |
Two keywords | Two keywords | One keyword |

Complexity |
High | Medium | Low |

Encryption Rules |
Uses four matrices to find substitutions | Uses two matrices to find substitutions | Uses one matrix to find substitutions |

## Transposition and Substitution

The *Traité Élémentaire de Cryptographie* by Felix Delastelle explores in depth the methods of
transposition and substitution in cryptography. Transposition involves altering the positions of the
letters in the plaintext to encrypt the message. For example, reversing the order of letters, as in the
case of "Paul est parti pour Lyon" becoming "NOYLRUOPITRAPTSELUAP". Another method is grouping, where
the plaintext is arranged into a predetermined shape, and then letters are extracted in an agreed-upon
order to form the ciphertext.

**Original en français:**

"L'inversion consiste à transposer ou déplacer les lettres du texte clair suivant une méthode convenue
entre les correspondants, de telle sorte qu'il soit facile aux initiés de rétablir l'ordre primitif."

**Translation in English:**

"Inversion consists of transposing or moving the letters of the clear text according to an agreed-upon
method between the correspondents so that it is easy for the initiated to restore the original order."

**Original en français:**

Le renversement s'effectue en écrivant les lettres du clair en sens inverse de l'ordre normal, la
dernière lettre devenant la première, l'avant-dernière la seconde, l'antépénultième la troisième, etc.
Exemple: "Paul est parti pour Lyon" s'écrira "NOYLRUOPITRAPTSELUAP". Le renversement peut s'appliquer
soit au texte entier, soit successivement à chaque mot ou à des groupes d'un nombre de lettres convenu.
La phrase ci-dessus, divisée en deux groupes de dix lettres, donnerait: "RAPTSELUAPNOYLRUOPIT". Et en
quatre groupes de cinq lettres: "ELUAPRAPTSUOPITNOYLR".

**Translation in English:**

The reversal is carried out by writing the letters of the clear text in the opposite direction to the
normal order, the last letter becoming the first, the penultimate the second, the antepenultimate the
third, etc. Example: "Paul est parti pour Lyon" would be written "NOYLRUOPITRAPTSELUAP". The reversal
can be applied either to the entire text, or successively to each word or to groups of an agreed number
of letters. The sentence above, divided into two groups of ten letters, would give:
"RAPTSELUAPNOYLRUOPIT". And in four groups of five letters: "ELUAPRAPTSUOPITNOYLR".

**Original en français:**

"De nombreux systèmes ont été imaginés dans ce but: 1 Renversement des lettres; 2 Groupements divers; 3
Carrés et grilles; 4 Méthodes diverses."

**Translation in English:**

"Many systems have been devised for this purpose: 1 Reversal of letters; 2 Various groupings; 3 Squares
and grids; 4 Various methods."

**Original en français:**

"Il est évident que le renversement ne donne aucune sécurité au point de vue du secret, les dépêches
écrites dans ce système ne résistant pas à un examen un peu sérieux."

**Translation in English:**

"It is evident that reversal provides no security from a secrecy point of view; messages written in this
system do not withstand even a slightly serious examination."