Encrypt or decrypt the message using the key.
(message, key, mode)
| 91 | |
| 92 | |
| 93 | def translateMessage(message, key, mode): |
| 94 | """Encrypt or decrypt the message using the key.""" |
| 95 | translated = '' |
| 96 | charsA = LETTERS |
| 97 | charsB = key |
| 98 | if mode == 'decrypt': |
| 99 | # For decrypting, we can use the same code as encrypting. We |
| 100 | # just need to swap where the key and LETTERS strings are used. |
| 101 | charsA, charsB = charsB, charsA |
| 102 | |
| 103 | # Loop through each symbol in the message: |
| 104 | for symbol in message: |
| 105 | if symbol.upper() in charsA: |
| 106 | # Encrypt/decrypt the symbol: |
| 107 | symIndex = charsA.find(symbol.upper()) |
| 108 | if symbol.isupper(): |
| 109 | translated += charsB[symIndex].upper() |
| 110 | else: |
| 111 | translated += charsB[symIndex].lower() |
| 112 | else: |
| 113 | # The symbol is not in LETTERS, just add it unchanged. |
| 114 | translated += symbol |
| 115 | |
| 116 | return translated |
| 117 | |
| 118 | |
| 119 | def generateRandomKey(): |
no outgoing calls
no test coverage detected