MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / decode

Function decode

ciphers/playfair_cipher.py:83–102  ·  view source on GitHub ↗
(ciphertext, key)

Source from the content-addressed store, hash-verified

81
82
83def decode(ciphertext, key):
84 table = generate_table(key)
85 plaintext = ""
86
87 # https://en.wikipedia.org/wiki/Playfair_cipher#Description
88 for char1, char2 in chunker(ciphertext, 2):
89 row1, col1 = divmod(table.index(char1), 5)
90 row2, col2 = divmod(table.index(char2), 5)
91
92 if row1 == row2:
93 plaintext += table[row1*5+(col1-1)%5]
94 plaintext += table[row2*5+(col2-1)%5]
95 elif col1 == col2:
96 plaintext += table[((row1-1)%5)*5+col1]
97 plaintext += table[((row2-1)%5)*5+col2]
98 else: # rectangle
99 plaintext += table[row1*5+col2]
100 plaintext += table[row2*5+col1]
101
102 return plaintext

Callers

nothing calls this directly

Calls 2

generate_tableFunction · 0.85
chunkerFunction · 0.85

Tested by

no test coverage detected