Game Development Reference
In-Depth Information
6. while True:
7. print('Do you wish to encrypt or decrypt or brute
force a message?')
8. mode = input().lower()
9. if mode in 'encrypt e decrypt d brute b '.split():
10. return mode[0]
11. else:
12. print('Enter either "encrypt" or "e" or
"decrypt" or "d" or "brute" or "b" .')
This will let us select "brute force" as a mode for our program. Then modify and add the
following changes to the main part of the program:
52. mode = getMode()
53. message = getMessage()
54. if mode[0] != 'b':
55. key = getKey()
56.
57. print('Your translated text is:')
58. if mode[0] != 'b':
59. print(getTranslatedMessage(mode, message, key))
60. else:
61. for key in range(1, MAX_KEY_SIZE + 1):
62. print(key, getTranslatedMessage('decrypt',
message, key))
These changes make our program ask the user for a key if they are not in "brute force"
mode. If they are not in "brute force" mode, then the original getTranslatedMessage
() call is made and the translated string is printed.
However, otherwise we are in "brute force" mode, and we run a
getTranslatedMessage() loop that iterates from 1 all the way up to
MAX_KEY_SIZE (which is 26 ). Remember that when the range() function returns a list
of integers up to but not including the second parameter, which is why we have + 1 . This
program will print out every possible translation of the message (including the key number
used in the translation). Here is a sample run of this modified program:
Search WWH ::




Custom Search