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

Function encode

ciphers/playfair_cipher.py:60–80  ·  view source on GitHub ↗
(plaintext, key)

Source from the content-addressed store, hash-verified

58 return table
59
60def encode(plaintext, key):
61 table = generate_table(key)
62 plaintext = prepare_input(plaintext)
63 ciphertext = ""
64
65 # https://en.wikipedia.org/wiki/Playfair_cipher#Description
66 for char1, char2 in chunker(plaintext, 2):
67 row1, col1 = divmod(table.index(char1), 5)
68 row2, col2 = divmod(table.index(char2), 5)
69
70 if row1 == row2:
71 ciphertext += table[row1*5+(col1+1)%5]
72 ciphertext += table[row2*5+(col2+1)%5]
73 elif col1 == col2:
74 ciphertext += table[((row1+1)%5)*5+col1]
75 ciphertext += table[((row2+1)%5)*5+col2]
76 else: # rectangle
77 ciphertext += table[row1*5+col2]
78 ciphertext += table[row2*5+col1]
79
80 return ciphertext
81
82
83def decode(ciphertext, key):

Callers

nothing calls this directly

Calls 3

generate_tableFunction · 0.85
prepare_inputFunction · 0.85
chunkerFunction · 0.85

Tested by

no test coverage detected