MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / translateMessage

Function translateMessage

src/gamesbyexample/simplesubcipher.py:93–116  ·  view source on GitHub ↗

Encrypt or decrypt the message using the key.

(message, key, mode)

Source from the content-addressed store, hash-verified

91
92
93def 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
119def generateRandomKey():

Callers 2

encryptMessageFunction · 0.70
decryptMessageFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected