Back to Encryption Techniques

Playfair Cipher Explorer

Interactive Playfair Cipher

Enter your text and key to see the Playfair cipher in action.

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

About Playfair Cipher

Description

The Playfair cipher is a manual symmetric encryption technique and was the first literal digraph substitution cipher. It employs a 5x5 grid of letters constructed using a keyword.

How it Works

  1. Choose a keyword and remove duplicate letters.
  2. Fill a 5x5 matrix with the keyword letters first, then the remaining alphabet (I and J are typically combined).
  3. Divide the plaintext into pairs of letters (digraphs).
  4. For each pair:
    • If both letters are in the same row, replace with letters to their right (wrapping around).
    • If both letters are in the same column, replace with letters below (wrapping around).
    • If neither, replace with letters that form a rectangle in the grid.

Strengths

  • Resistant to frequency analysis of individual letters
  • Relatively quick for manual encryption compared to more complex ciphers
  • Provides better security than simple monoalphabetic substitution ciphers

Weaknesses

  • Vulnerable to known-plaintext attacks
  • Digraph frequency analysis can be used to break the cipher
  • Limited keyspace compared to modern encryption standards

Historical Context

The Playfair cipher was invented in 1854 by Charles Wheatstone, but it bears the name of Lord Playfair who promoted its use. It was used for tactical purposes by British forces in the Second Boer War and in World War I. It was also used by the Australians during World War II.

Modern Applications

While the Playfair cipher is not secure by modern standards, it remains an important educational tool in cryptography. It introduces concepts such as digraph substitution and the use of a key to create a cipher alphabet, which are foundational to understanding more complex encryption systems.

PlayFair Cipher Implementations

Examples of PlayFair Cipher implementation in various programming languages.


def create_matrix(key):
    key = key.upper().replace("J", "I")
    matrix = []
    seen = set()
    for char in key:
        if char not in seen and char.isalpha():
            seen.add(char)
            matrix.append(char)
    for char in range(65, 91):
        c = chr(char)
        if c != 'J' and c not in seen:
            seen.add(c)
            matrix.append(c)
    return [matrix[i:i+5] for i in range(0, len(matrix), 5)]

def find_position(matrix, char):
    for i, row in enumerate(matrix):
        if char in row:
            return i, row.index(char)
    return -1, -1

def prepare_text(text):
    text = text.upper().replace("J", "I")
    text = "".join([char if char.isalpha() else "" for char in text])
    if len(text) % 2 != 0:
        text += "X"
    return [text[i:i+2] for i in range(0, len(text), 2)]

def playfair_cipher(text, key, encrypt=True):
    matrix = create_matrix(key)
    pairs = prepare_text(text)
    result = []

    for pair in pairs:
        row1, col1 = find_position(matrix, pair[0])
        row2, col2 = find_position(matrix, pair[1])

        if row1 == row2:
            col1 = (col1 + (1 if encrypt else -1)) % 5
            col2 = (col2 + (1 if encrypt else -1)) % 5
        elif col1 == col2:
            row1 = (row1 + (1 if encrypt else -1)) % 5
            row2 = (row2 + (1 if encrypt else -1)) % 5
        else:
            col1, col2 = col2, col1

        result.append(matrix[row1][col1])
        result.append(matrix[row2][col2])

    return ''.join(result)

# Example usage
plaintext = "HELLO WORLD"
key = "KEYWORD"
encrypted = playfair_cipher(plaintext, key)
decrypted = playfair_cipher(encrypted, key, encrypt=False)
print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")