The Algorithms logo
算法
关于我们捐赠

Playfair 密码

A
R
"""
https://en.wikipedia.org/wiki/Playfair_cipher#Description

The Playfair cipher was developed by Charles Wheatstone in 1854
It's use was heavily promotedby Lord Playfair, hence its name

Some features of the Playfair cipher are:

1) It was the first literal diagram substitution cipher
2) It is a manual symmetric encryption technique
3) It is a multiple letter encryption cipher

The implementation in the code below encodes alphabets only.
It removes spaces, special characters and numbers from the
code.

Playfair is no longer used by military forces because of known
insecurities and of the advent of automated encryption devices.
This cipher is regarded as insecure since before World War I.
"""

import itertools
import string
from collections.abc 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:
    """
    Encode the given plaintext using the Playfair cipher.
    Takes the plaintext and the key as input and returns the encoded string.

    >>> encode("Hello", "MONARCHY")
    'CFSUPM'
    >>> encode("attack on the left flank", "EMERGENCY")
    'DQZSBYFSDZFMFNLOHFDRSG'
    >>> encode("Sorry!", "SPECIAL")
    'AVXETX'
    >>> encode("Number 1", "NUMBER")
    'UMBENF'
    >>> encode("Photosynthesis!", "THE SUN")
    'OEMHQHVCHESUKE'
    """

    table = generate_table(key)
    plaintext = prepare_input(plaintext)
    ciphertext = ""

    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:
    """
    Decode the input string using the provided key.

    >>> decode("BMZFAZRZDH", "HAZARD")
    'FIREHAZARD'
    >>> decode("HNBWBPQT", "AUTOMOBILE")
    'DRIVINGX'
    >>> decode("SLYSSAQS", "CASTLE")
    'ATXTACKX'
    """

    table = generate_table(key)
    plaintext = ""

    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


if __name__ == "__main__":
    import doctest

    doctest.testmod()

    print("Encoded:", encode("BYE AND THANKS", "GREETING"))
    print("Decoded:", decode("CXRBANRLBALQ", "GREETING"))
关于此算法

Playfair 密码是由 查尔斯·惠斯通 于 1854 年发明的,但以推广使用该密码的普莱费尔勋爵命名。

Playfair 密码是第一个实用的二字母替换密码。与传统的密码不同,在 Playfair 密码中,我们加密的是一对字母(二字母组),而不是单个字母。一个 5 × 5 的字母网格被用作密钥方格。25 个字母中的每一个都是唯一的,其中一个字母(通常是 J)被省略。如果明文中包含 J,则将其替换为 I 或反之。密钥方格中的初始字母是密钥中唯一字母的顺序,然后是剩余字母的顺序。

示例

假设我们以以下为例

明文(PT):instruments,密钥:monarchy

规则

  1. 如果两个字母都在同一列,则取每个字母下面的字母(如果在底部,则回到顶部)。
    Diagraph: "me"
    Encrypted Text: cl
    Encryption: 
      m -> c
      e -> l
  1. 如果两个字母都在同一行,则取每个字母右边的字母(如果在最右边,则回到最左边)。
    Diagraph: "st"
    Encrypted Text: tl
    Encryption: 
      s -> t
      t -> l
  1. 如果上述规则都不适用,则用这两个字母形成一个矩形,并取矩形水平对角上的字母。
    Diagraph: "nt"
    Encrypted Text: rq
    Encryption: 
      n -> r
      t -> q

上述规则用于加密。可以反过来应用于解密。

步骤

加密

  1. 我们必须从密钥生成一个 5 × 5 矩阵,如下所示
   [m o n a r]
   [c h y b d]
   [e f g i k]
   [l p q s t]
   [u v w x z]
  1. 将明文分成二字母组(两字母对)。如果字母数为奇数,则在最后一个字母后面添加一个 X。字母对不能由相同字母构成。如果出现这种情况,则通过在重复字母之间添加一个 X 来拆分字母对。
   'in' 'st' 'ru' 'me' 'nt' 'sx'
  1. 现在,我们需要按照加密规则进行操作,如下所示
    Plain Text: instrumentsx
    key: monarchy
    Encryption: 
      i -> g
      n -> a
      s -> t
      t -> l
      r -> m
      u -> z
      m -> c
      e -> l
      n -> r
      t -> q
      s -> x
      x -> a

因此,我们将获得加密文本 gatlmzclrqxa

解密

  1. 我们必须从密钥生成一个 5 × 5 矩阵,如下所示
   [m o n a r]
   [c h y b d]
   [e f g i k]
   [l p q s t]
   [u v w x z]
  1. 我们需要像加密时对明文一样拆分密文
   'ga' 'tl' 'mz' 'cl' 'rq' 'xa'
  1. 对于前面的密文 gatlmzclrqxa,按照规则,我们得到
    Plain Text: gatlmzclrqtx
    key: monarchy
    Decryption:
      ga -> in
      tl -> st
      mz -> ru
      cl -> me
      rq -> nt
      xa -> sx

因此,我们将获得解密文本 instrumentsx

视频讲解