The Algorithms logo
The Algorithms
AboutDonate

Playfair Cipher

R
D
J
W
a
9
e
c
and 1 more contributors
import itertools
import string
from typing import Generator, Iterable


def chunker(seq: Iterable[str], size: int) -> Generator[tuple[str, ...], None, None]:
    it = iter(seq)
    while True:
        chunk = tuple(itertools.islice(it, size))
        if not chunk:
            return
        yield chunk


def prepare_input(dirty: str) -> str:
    """
    Prepare the plaintext by up-casing it
    and separating repeated letters with X's
    """

    dirty = "".join([c.upper() for c in dirty if c in string.ascii_letters])
    clean = ""

    if len(dirty) < 2:
        return dirty

    for i in range(len(dirty) - 1):
        clean += dirty[i]

        if dirty[i] == dirty[i + 1]:
            clean += "X"

    clean += dirty[-1]

    if len(clean) & 1:
        clean += "X"

    return clean


def generate_table(key: str) -> list[str]:

    # I and J are used interchangeably to allow
    # us to use a 5x5 table (25 letters)
    alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
    # we're using a list instead of a '2d' array because it makes the math
    # for setting up the table and doing the actual encoding/decoding simpler
    table = []

    # copy key chars into the table if they are in `alphabet` ignoring duplicates
    for char in key.upper():
        if char not in table and char in alphabet:
            table.append(char)

    # fill the rest of the table in with the remaining alphabet chars
    for char in alphabet:
        if char not in table:
            table.append(char)

    return table


def encode(plaintext: str, key: str) -> str:
    table = generate_table(key)
    plaintext = prepare_input(plaintext)
    ciphertext = ""

    # https://en.wikipedia.org/wiki/Playfair_cipher#Description
    for char1, char2 in chunker(plaintext, 2):
        row1, col1 = divmod(table.index(char1), 5)
        row2, col2 = divmod(table.index(char2), 5)

        if row1 == row2:
            ciphertext += table[row1 * 5 + (col1 + 1) % 5]
            ciphertext += table[row2 * 5 + (col2 + 1) % 5]
        elif col1 == col2:
            ciphertext += table[((row1 + 1) % 5) * 5 + col1]
            ciphertext += table[((row2 + 1) % 5) * 5 + col2]
        else:  # rectangle
            ciphertext += table[row1 * 5 + col2]
            ciphertext += table[row2 * 5 + col1]

    return ciphertext


def decode(ciphertext: str, key: str) -> str:
    table = generate_table(key)
    plaintext = ""

    # https://en.wikipedia.org/wiki/Playfair_cipher#Description
    for char1, char2 in chunker(ciphertext, 2):
        row1, col1 = divmod(table.index(char1), 5)
        row2, col2 = divmod(table.index(char2), 5)

        if row1 == row2:
            plaintext += table[row1 * 5 + (col1 - 1) % 5]
            plaintext += table[row2 * 5 + (col2 - 1) % 5]
        elif col1 == col2:
            plaintext += table[((row1 - 1) % 5) * 5 + col1]
            plaintext += table[((row2 - 1) % 5) * 5 + col2]
        else:  # rectangle
            plaintext += table[row1 * 5 + col2]
            plaintext += table[row2 * 5 + col1]

    return plaintext
About this Algorithm

The Playfair cipher was invented in 1854 by Charles Wheatstone but was named after Lord Playfair who promoted the use of the cipher.

The Playfair cipher was the first practical digraph substitution cipher. In Playfair cipher unlike traditional cipher, we encrypt a pair of alphabets(digraphs) instead of a single alphabet. A 5 × 5 grid of alphabets was used as the key-square. Each of the 25 alphabets is unique and one letter of the alphabet (usually J) is omitted from the table. If the plaintext contains J, then it is replaced by I. The initial alphabets in the key square are the unique alphabets of the key in the order in which they appear followed by the remaining letters of the alphabet in order.

Example

Suppose we take an example as:

Plain Text (PT): instruments, key: monarchy

Rules

  1. If both the letters are in the same column, take the letter below each one (going back to the top if at the bottom).
    Diagraph: "me"
    Encrypted Text: cl
    Encryption: 
      m -> c
      e -> l
  1. If both the letters are in the same row, take the letter to the right of each one (going back to the leftmost only if it's at the rightmost position).
    Diagraph: "st"
    Encrypted Text: tl
    Encryption: 
      s -> t
      t -> l
  1. If neither of the above rules is true, form a rectangle with the two letters, and take the letters on the horizontal opposite corner of the rectangle.
    Diagraph: "nt"
    Encrypted Text: rq
    Encryption: 
      n -> r
      t -> q

The rules above are used for Encryption. Can be applied vice-versa for Decryption.

Steps

Encryption

  1. We have to generate a 5 × 5 matrix from the key as
   [m o n a r]
   [c h y b d]
   [e f g k i]
   [l p q s t]
   [u v w x z]
  1. Split the plaintext in digraphs(pair of two). If there is an odd number of letters, a Z is added to the last letter. Pair cannot be made with same letter. Break the letter in single and add a bogus letter to the previous letter.
   'in' 'st' 'ru' 'me' 'nt' 'sz'
  1. Now, we need to follow the rules for encrypting and do as follows:
    Plain Text: instrumentsz
    key: monarchy
    Encryption: 
      i -> g
      n -> a
      s -> t
      t -> l
      r -> m
      u -> z
      m -> c
      e -> l
      n -> r
      t -> q
      s -> t
      z -> x

So we will get the encrypted text as gatlmzclrqtx.

Decryption

  1. We have to generate a 5 × 5 matrix from the key as
   [m o n a r]
   [c h y b d]
   [e f g k i]
   [l p q s t]
   [u v w x z]
  1. We need to split the ciphertext as done for plaintext while encrypting
   'ga' 'tl' 'mz' 'cl' 'rq' 'tx'
  1. For the previous Cipher Text gatlmzclrqtx, by following the rules we get:
    Plain Text: gatlmzclrqtx
    key: monarchy
    Decryption:
      ga -> in
      tl -> st
      mz -> ru
      cl -> me
      rq -> nt
      tx -> sz

So we will get the encrypted text as instrumentsz.

Implementations

Video Explanation