Playfair Cipher
Operation steps
- Construct a 5×5 letter matrix based on
- The matrix is constructed using a keyword (key)
- Then from left to right, from top to bottom; fill in the letters of the key in sequence (note: repeated letters in the key are not filled in), and then fill in the other letters in alphabetical order
- Letters I and J count as one letter
For example: take the keyword (key): monument, then the letter matrix is as shown below (the matrix can only hold 25 letters, I and J are the same)
Encryption rules
The Playfair encryption algorithm first groups the plaintext into groups of two letters, and then finds the corresponding ciphertext in the matrix
- If the same letters appear in a group in the plaintext, insert a padding letter (eg: z) into the repeated plaintext letters to separate them and then regroup them (eg: balloon is regrouped intoba lz lo on)
- If there is only one letter when grouping into the last group, add the letter z
- If the plaintext letters are in the same row in the matrix, the loop will take the next letter to the right as the ciphertext (the next rightmost letter in the matrix is the first leftmost letter) (eg: ar is encrypted as RM)
- If the plaintext letters are in the same column in the matrix, then the next letter below it is looped to be the ciphertext (the next letter at the bottom of the matrix is the first one at the top) (eg: mu is encrypted as CM)
- If the plaintext letters are in different rows and different columns in the matrix, the letters in the same row and in the same column as another letter in the same group are the ciphertext (eg: hs is encrypted as BP, ea is encrypted as IM or JM)
Note: If the same letters appear in the keywords (keys), you need to remove the duplicate keys (skip if there are duplicates)