Chapter 17 – The Simple Substitution Cipher 243
translateMessage() (the wrapped function) and returns the value
translateMessage() returns.
On line 45 notice that translateMessage() has the parameters key and message, but also
a third parameter named mode. When it calls translateMessage(), the call in
encryptMessage() function passes 'encrypt' for the mode parameter, and the call in
decryptMessage() function passes 'decrypt'. This is how the
translateMessage() function knows whether it should encrypt or decrypt the message it is
passed.
With these wrapper functions, someone who imports the simpleSubCipher.py program can call
functions named encryptMessage() and decryptMessage() like they do with all the
other cipher programs in this book. They might create a program that encrypts with various
ciphers, like below:
import affineCipher, simpleSubCipher, transpositionCipher
...some other code here...
ciphertext1 = affineCipher.encryptMessage(encKey1, 'Hello!')
ciphertext2 = transpositionCipher.encryptMessage(encKey2, 'Hello!')
ciphertext3 = simpleSubCipher.encryptMessage(encKey3, 'Hello!')
The wrapper functions give the simple substitution cipher program function names that are
consistent with the other cipher programs. Consistent names are very helpful, because it makes it
easier for someone familiar with one of the cipher programs in this book to already be familiar
with the other cipher programs. (You can even see that the first parameter was always made the
key and the second parameter is always the message.) This is the reason we have these wrapper
functions, because making the programmer call the translateMessage() function would be
inconsistent with the other programs.
The Program’s translateMessage() Function
simpleSubCipher.py
45. def translateMessage(key, message, mode):
46. translated = ''
47. charsA = LETTERS
48. charsB = key
49. if mode == 'decrypt':
50. # For decrypting, we can use the same code as encrypting. We
51. # just need to swap where the key and LETTERS strings are used.
52. charsA, charsB = charsB, charsA