(ciphertext, key)
| 81 | |
| 82 | |
| 83 | def 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 |
nothing calls this directly
no test coverage detected