| 37 | return clean |
| 38 | |
| 39 | def generate_table(key): |
| 40 | |
| 41 | # I and J are used interchangeably to allow |
| 42 | # us to use a 5x5 table (25 letters) |
| 43 | alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ" |
| 44 | # we're using a list instead of a '2d' array because it makes the math |
| 45 | # for setting up the table and doing the actual encoding/decoding simpler |
| 46 | table = [] |
| 47 | |
| 48 | # copy key chars into the table if they are in `alphabet` ignoring duplicates |
| 49 | for char in key.upper(): |
| 50 | if char not in table and char in alphabet: |
| 51 | table.append(char) |
| 52 | |
| 53 | # fill the rest of the table in with the remaining alphabet chars |
| 54 | for char in alphabet: |
| 55 | if char not in table: |
| 56 | table.append(char) |
| 57 | |
| 58 | return table |
| 59 | |
| 60 | def encode(plaintext, key): |
| 61 | table = generate_table(key) |