Skip to content

Commit fdb0635

Browse files
AnshuSharma111pre-commit-ci[bot]tianyizheng02
authored
added doctest to playfair_cipher.py (#10823)
* added doctest to playfair_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added newline to EOF andremoved trailing whitespace * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestions from code review --------- Co-authored-by: Keyboard-1 <142900182+Keyboard-1@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
1 parent a8b94ab commit fdb0635

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

ciphers/playfair_cipher.py

+57-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
"""
2+
https://en.wikipedia.org/wiki/Playfair_cipher#Description
3+
4+
The Playfair cipher was developed by Charles Wheatstone in 1854
5+
It's use was heavily promotedby Lord Playfair, hence its name
6+
7+
Some features of the Playfair cipher are:
8+
9+
1) It was the first literal diagram substitution cipher
10+
2) It is a manual symmetric encryption technique
11+
3) It is a multiple letter encryption cipher
12+
13+
The implementation in the code below encodes alphabets only.
14+
It removes spaces, special characters and numbers from the
15+
code.
16+
17+
Playfair is no longer used by military forces because of known
18+
insecurities and of the advent of automated encryption devices.
19+
This cipher is regarded as insecure since before World War I.
20+
"""
21+
122
import itertools
223
import string
324
from collections.abc import Generator, Iterable
@@ -60,11 +81,26 @@ def generate_table(key: str) -> list[str]:
6081

6182

6283
def encode(plaintext: str, key: str) -> str:
84+
"""
85+
Encode the given plaintext using the Playfair cipher.
86+
Takes the plaintext and the key as input and returns the encoded string.
87+
88+
>>> encode("Hello", "MONARCHY")
89+
'CFSUPM'
90+
>>> encode("attack on the left flank", "EMERGENCY")
91+
'DQZSBYFSDZFMFNLOHFDRSG'
92+
>>> encode("Sorry!", "SPECIAL")
93+
'AVXETX'
94+
>>> encode("Number 1", "NUMBER")
95+
'UMBENF'
96+
>>> encode("Photosynthesis!", "THE SUN")
97+
'OEMHQHVCHESUKE'
98+
"""
99+
63100
table = generate_table(key)
64101
plaintext = prepare_input(plaintext)
65102
ciphertext = ""
66103

67-
# https://en.wikipedia.org/wiki/Playfair_cipher#Description
68104
for char1, char2 in chunker(plaintext, 2):
69105
row1, col1 = divmod(table.index(char1), 5)
70106
row2, col2 = divmod(table.index(char2), 5)
@@ -83,10 +119,20 @@ def encode(plaintext: str, key: str) -> str:
83119

84120

85121
def decode(ciphertext: str, key: str) -> str:
122+
"""
123+
Decode the input string using the provided key.
124+
125+
>>> decode("BMZFAZRZDH", "HAZARD")
126+
'FIREHAZARD'
127+
>>> decode("HNBWBPQT", "AUTOMOBILE")
128+
'DRIVINGX'
129+
>>> decode("SLYSSAQS", "CASTLE")
130+
'ATXTACKX'
131+
"""
132+
86133
table = generate_table(key)
87134
plaintext = ""
88135

89-
# https://en.wikipedia.org/wiki/Playfair_cipher#Description
90136
for char1, char2 in chunker(ciphertext, 2):
91137
row1, col1 = divmod(table.index(char1), 5)
92138
row2, col2 = divmod(table.index(char2), 5)
@@ -102,3 +148,12 @@ def decode(ciphertext: str, key: str) -> str:
102148
plaintext += table[row2 * 5 + col1]
103149

104150
return plaintext
151+
152+
153+
if __name__ == "__main__":
154+
import doctest
155+
156+
doctest.testmod()
157+
158+
print("Encoded:", encode("BYE AND THANKS", "GREETING"))
159+
print("Decoded:", decode("CXRBANRLBALQ", "GREETING"))

0 commit comments

Comments
 (0)