How do I create a Playfair cipher

Playfair cipher

Around 1854, the physicist Charles Wheatstone invented a cipher in which not letters but groups of letters are encrypted. Andrew's Baron Playfair, a friend of Wheatstone, later published it under his name.

The algorithm is not based on the encryption of individual letters, but groups of letters of two characters each, which obscures the frequency distribution. As a basis, a 5 * 5 matrix is ​​created in which a keyword is first entered line by line without repeating characters that have already been entered. The remaining letters of the alphabet are then entered in the table. The letters i and j together occupy one element of the matrix. The plain text is divided into two letters - without spaces or punctuation marks - if there are two identical letters in a group, a filler letter, preferably the x, is inserted and divided again. If there is a single letter at the end, the filler letter is added again. A distinction is then made between three cases for coding:

-Both letters are in the same row: Each letter is encrypted by replacing it with the next one in the same line. If the plaintext letter is the last of the line, the first of the line is used for encryption.

-Both letters are in the same column: Each letter is encrypted by replacing it with the one below it in the same column. If the plaintext letter is the bottom of the column, it is encrypted with the top of the column.

-Both letters are neither in the same row nor in the same column: You go in the line of the first plain letter to the right or left to the column of the second letter. The letter there is the cipher for this. Do the same with the second letter. The decryption takes place in the same way, only in the opposite direction, instead of following it is preceding, instead of standing below it is taken above. In the third case alone, the same procedure can be used as with encryption.

For example: Key word: Extrawurst Matrix: EXTRAWUSBCDFGHIKLMNOP QVZY Plain text: I'll come on Wednesday Breakdown: ic hk om me am mi tx tw oc hx Secret text: oi dn kn kt to og rt eb yi fr The resulting ciphertext no longer shows the normal frequencies of natural language , because the distribution of the letter pairs is more even than that of the individual letters. However, this procedure can also be broken if there is enough encrypted text available in the same way. The particular advantage of the procedure, however, is that decoding part of the cipher does not allow conclusions to be drawn about the entire plaintext.