brute_force =========== Returns all the possible combinations of keys and the decoded strings in the form of a dictionary Parameters: ----------- * `input_string`: the cipher-text that needs to be used during brute-force Optional: * `alphabet` (``None``)
(input_string: str, alphabet: str | None = None)
| 161 | |
| 162 | |
| 163 | def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str]: |
| 164 | """ |
| 165 | brute_force |
| 166 | =========== |
| 167 | |
| 168 | Returns all the possible combinations of keys and the decoded strings in the |
| 169 | form of a dictionary |
| 170 | |
| 171 | Parameters: |
| 172 | ----------- |
| 173 | |
| 174 | * `input_string`: the cipher-text that needs to be used during brute-force |
| 175 | |
| 176 | Optional: |
| 177 | |
| 178 | * `alphabet` (``None``): the alphabet used to decode the cipher, if not |
| 179 | specified, the standard english alphabet with upper and lowercase |
| 180 | letters is used |
| 181 | |
| 182 | More about brute force |
| 183 | ====================== |
| 184 | |
| 185 | Brute force is when a person intercepts a message or password, not knowing |
| 186 | the key and tries every single combination. This is easy with the caesar |
| 187 | cipher since there are only all the letters in the alphabet. The more |
| 188 | complex the cipher, the larger amount of time it will take to do brute force |
| 189 | |
| 190 | Ex: |
| 191 | Say we have a ``5`` letter alphabet (``abcde``), for simplicity and we intercepted |
| 192 | the following message: ``dbc``, |
| 193 | we could then just write out every combination: |
| 194 | ``ecd``... and so on, until we reach a combination that makes sense: |
| 195 | ``cab`` |
| 196 | |
| 197 | Further reading |
| 198 | =============== |
| 199 | |
| 200 | * https://en.wikipedia.org/wiki/Brute_force |
| 201 | |
| 202 | Doctests |
| 203 | ======== |
| 204 | |
| 205 | >>> brute_force("jFyuMy xIH'N vLONy zILwy Gy!")[20] |
| 206 | "Please don't brute force me!" |
| 207 | |
| 208 | >>> brute_force(1) |
| 209 | Traceback (most recent call last): |
| 210 | TypeError: 'int' object is not iterable |
| 211 | """ |
| 212 | # Set default alphabet to lower and upper case english chars |
| 213 | alpha = alphabet or ascii_letters |
| 214 | |
| 215 | # To store data on all the combinations |
| 216 | brute_force_data = {} |
| 217 | |
| 218 | # Cycle through each combination |
| 219 | for key in range(1, len(alpha) + 1): |
| 220 | # Decrypt the message and store the result in the data |
no test coverage detected