(plaintext, key)
| 58 | return table |
| 59 | |
| 60 | def 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 | |
| 83 | def decode(ciphertext, key): |
nothing calls this directly
no test coverage detected