Skip to content

Commit 0a4868a

Browse files
Add playfair-cipher.md (#133)
* Create playfair-cipher.md * Add playfair-cipher.md * Update en/Ciphers/playfair-cipher.md Co-authored-by: David Leal <halfpacho@gmail.com> * Update en/Ciphers/playfair-cipher.md Co-authored-by: David Leal <halfpacho@gmail.com> * Update en/Ciphers/playfair-cipher.md Co-authored-by: David Leal <halfpacho@gmail.com> * Update en/Ciphers/playfair-cipher.md Co-authored-by: David Leal <halfpacho@gmail.com> * Update en/Ciphers/playfair-cipher.md Co-authored-by: David Leal <halfpacho@gmail.com> * Update en/Ciphers/playfair-cipher.md Co-authored-by: David Leal <halfpacho@gmail.com> * Update en/Ciphers/playfair-cipher.md Co-authored-by: David Leal <halfpacho@gmail.com> * Update playfair-cipher.md * Update en/Ciphers/playfair-cipher.md Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: David Leal <halfpacho@gmail.com>
1 parent dae2d48 commit 0a4868a

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

en/Ciphers/playfair-cipher.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Playfair Cipher
2+
3+
The Playfair cipher was invented in 1854 by [Charles Wheatstone](https://en.wikipedia.org/wiki/Charles_Wheatstone) but was named after Lord Playfair who promoted the use of the cipher.
4+
5+
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.
6+
7+
## Example
8+
9+
Suppose we take an example as:
10+
11+
Plain Text (PT): instruments, key: `monarchy`
12+
13+
## Rules
14+
15+
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).
16+
17+
```
18+
Diagraph: "me"
19+
Encrypted Text: cl
20+
Encryption:
21+
m -> c
22+
e -> l
23+
```
24+
25+
2. 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).
26+
27+
```
28+
Diagraph: "st"
29+
Encrypted Text: tl
30+
Encryption:
31+
s -> t
32+
t -> l
33+
```
34+
35+
3. 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.
36+
37+
```
38+
Diagraph: "nt"
39+
Encrypted Text: rq
40+
Encryption:
41+
n -> r
42+
t -> q
43+
```
44+
45+
The rules above are used for Encryption. Can be applied vice-versa for Decryption.
46+
47+
## Steps
48+
49+
### Encryption
50+
51+
1. We have to generate a `5 × 5` matrix from the key as
52+
53+
```
54+
[m o n a r]
55+
[c h y b d]
56+
[e f g k i]
57+
[l p q s t]
58+
[u v w x z]
59+
```
60+
61+
2. 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.
62+
63+
```
64+
'in' 'st' 'ru' 'me' 'nt' 'sz'
65+
```
66+
67+
3. Now, we need to follow the rules for encrypting and do as follows:
68+
69+
```
70+
Plain Text: instrumentsz
71+
key: monarchy
72+
Encryption:
73+
i -> g
74+
n -> a
75+
s -> t
76+
t -> l
77+
r -> m
78+
u -> z
79+
m -> c
80+
e -> l
81+
n -> r
82+
t -> q
83+
s -> t
84+
z -> x
85+
```
86+
So we will get the encrypted text as **gatlmzclrqtx**.
87+
88+
### Decryption
89+
90+
1. We have to generate a `5 × 5` matrix from the key as
91+
92+
```
93+
[m o n a r]
94+
[c h y b d]
95+
[e f g k i]
96+
[l p q s t]
97+
[u v w x z]
98+
```
99+
100+
2. We need to split the ciphertext as done for plaintext while encrypting
101+
```
102+
'ga' 'tl' 'mz' 'cl' 'rq' 'tx'
103+
```
104+
105+
3. For the previous Cipher Text **gatlmzclrqtx**, by following the rules we get:
106+
107+
```
108+
Plain Text: gatlmzclrqtx
109+
key: monarchy
110+
Decryption:
111+
ga -> in
112+
tl -> st
113+
mz -> ru
114+
cl -> me
115+
rq -> nt
116+
tx -> sz
117+
```
118+
So we will get the encrypted text as **instrumentsz**.
119+
120+
## Implementations
121+
122+
- [**Python**](https://github.com/TheAlgorithms/Python/blob/master/ciphers/playfair_cipher.py)
123+
124+
## Video Explanation
125+
126+
- [**Video explanation of the Playfair Cipher algorithm**](https://www.youtube.com/watch?v=UURjVI5cw4g)

0 commit comments

Comments
 (0)