forked from asweigart/codebreaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesarBreaker.py
38 lines (27 loc) · 1.24 KB
/
caesarBreaker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Caesar Cipher Breaker
# http://inventwithpython.com/codebreaker (BSD Licensed)
message = 'GUVF VF ZL FRPERG ZRFFNTR.'
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# loop through every possible key
for key in range(len(SYMBOLS)):
# It is important to set translated to the blank string so that the
# previous iteration's value for translated is cleared.
translated = ''
# The rest of the program is the same as the original Caesar program:
# run the encryption/decryption code on each symbol in the message string
for symbol in message:
# get the number of the symbol
num = SYMBOLS.find(symbol)
# -1 means the symbol in the message was not found in SYMBOLS
if num != -1:
num = num - key
# handle the wrap around if num is 26 or larger of less than 0
if num < 0:
num = num + len(SYMBOLS)
# add encrypted/decrypted number's symbol at the end of translated
translated = translated + SYMBOLS[num]
else:
# just add the symbol without encrypting/decrypting
translated = translated + symbol
# display the current key being tested, along with its decryption
print('Key #%s: %s' % (key, translated))