|
| 1 | +Substitution Cipher |
| 2 | +=================== |
| 3 | + |
| 4 | +In this chapter you learn: |
| 5 | +-------------------------- |
| 6 | + |
| 7 | +==== ============================================== |
| 8 | +area topic |
| 9 | +==== ============================================== |
| 10 | +🚀 encrypt text with a substitution cipher |
| 11 | +⚙ convert strings to lists and back |
| 12 | +⚙ use a random seed |
| 13 | +💡 use the ``random`` module |
| 14 | +💡 use the ``string`` module |
| 15 | +💡 use the ``zip`` function |
| 16 | +🔀 use the ``dict(zip())`` pattern |
| 17 | +🐞 fix IndexErrors |
| 18 | +==== ============================================== |
| 19 | + |
| 20 | +Exercise 1: Random string |
| 21 | +------------------------- |
| 22 | + |
| 23 | +The following code creates a random chiffre that we will use for encryption. |
| 24 | +Insert ``alphabet``, ``chars``, ``join`` and ``random`` into the code: |
| 25 | + |
| 26 | +.. code:: python3 |
| 27 | +
|
| 28 | + import random |
| 29 | +
|
| 30 | + alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 31 | +
|
| 32 | + ___ = list(___) |
| 33 | + ___.shuffle(chars) |
| 34 | + chiffre = "".___(chars) |
| 35 | + print(chiffre) |
| 36 | +
|
| 37 | +Run the program a couple of times. |
| 38 | + |
| 39 | + |
| 40 | +Exercise 2: Random seed |
| 41 | +----------------------- |
| 42 | + |
| 43 | +Add the following line after the import: |
| 44 | + |
| 45 | +.. code:: python3 |
| 46 | +
|
| 47 | + random.seed(77) |
| 48 | +
|
| 49 | +Run the program a couple of times. |
| 50 | +What changes? |
| 51 | + |
| 52 | +Also try running the program with different numbers. |
| 53 | + |
| 54 | + |
| 55 | +Exercise 3: Chiffre indices |
| 56 | +--------------------------- |
| 57 | + |
| 58 | +Output each character from the chiffre and its index |
| 59 | +Simplify the following code using the function ``enumerate()``: |
| 60 | + |
| 61 | +.. code:: python3 |
| 62 | +
|
| 63 | + i = 0 |
| 64 | + for char in chiffre: |
| 65 | + print(i, char) |
| 66 | + i += 1 |
| 67 | +
|
| 68 | +
|
| 69 | +Exercise 4: Lookup dictionary |
| 70 | +----------------------------- |
| 71 | + |
| 72 | +Create a dictionary that maps unencrypted to encrypted characters: |
| 73 | + |
| 74 | +.. code:: python3 |
| 75 | +
|
| 76 | + lookup = {} |
| 77 | + i = 0 |
| 78 | + while i < len(alphabet): |
| 79 | + char = alphabet[i] |
| 80 | + lookup[char] = chiffre[i] |
| 81 | + i += 1 |
| 82 | + print(lookup) |
| 83 | +
|
| 84 | +Execute the program slowly on paper to make sure you understand the index operations. |
| 85 | + |
| 86 | +Exercise 5: Zip |
| 87 | +--------------- |
| 88 | + |
| 89 | +Use the following code to make the program above simpler: |
| 90 | + |
| 91 | +.. code:: python3 |
| 92 | +
|
| 93 | + for char, chif in zip(alphabet, chiffre) |
| 94 | + print(char, chif) |
| 95 | +
|
| 96 | +Once you understand what happens, also try: |
| 97 | + |
| 98 | +.. code:: python3 |
| 99 | +
|
| 100 | + lookup = dict(zip(alphabet, chiffer)) |
| 101 | +
|
| 102 | +
|
| 103 | +Exercise 6: Encrypt |
| 104 | +------------------- |
| 105 | + |
| 106 | +Use the lookup dictionary to encrypt a message. |
| 107 | +Use the following lines as a starting point: |
| 108 | + |
| 109 | +.. code:: python3 |
| 110 | +
|
| 111 | + message = ... |
| 112 | + result = "" |
| 113 | +
|
| 114 | + for char in message: |
| 115 | + result += ... |
| 116 | +- |
| 117 | +Exercise 7: Debugging |
| 118 | +--------------------- |
| 119 | + |
| 120 | +What happens when you enter lowercase letters or special characters in the message? |
| 121 | + |
| 122 | +Try the expression: |
| 123 | + |
| 124 | +.. code:: python3 |
| 125 | +
|
| 126 | + print(lookup.get(";", "X")) |
| 127 | +
|
| 128 | +Fix the problem. |
| 129 | + |
| 130 | + |
| 131 | +Exercise 7: Decrypt |
| 132 | +------------------- |
| 133 | + |
| 134 | +Decrypt a message when all you know is the encrypted message and the random seed. |
| 135 | + |
| 136 | + |
| 137 | +Reflection Questions |
| 138 | +-------------------- |
| 139 | + |
| 140 | +- what does the ``enumerate()`` function do? |
| 141 | +- what does the ``zip()`` function do? |
| 142 | +- is a substitution cipher rather safe or unsafe? |
| 143 | +- what is **symmetric encryption**? |
| 144 | + |
| 145 | +.. seealso:: |
| 146 | + |
| 147 | + **Further Reading** |
| 148 | + |
| 149 | + - `Kerckhoffs Principle <https://en.wikipedia.org/wiki/Kerckhoffs%27s_principle>`__ |
0 commit comments