-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesarHacker.py
39 lines (31 loc) · 1.47 KB
/
caesarHacker.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
39
"""Caesar Cipher Hacker, by Al Sweigart [email protected]
Tjos program hacks messages encrypted with the Caesar cipher by doing
a brute force attack against every possible key.
More info at:
https://en.wikipedia.org/wikiCaesar_cipher#Breaking_the_cipher
View this code at https://nostarch.com/big-book-small-python-projects
Tags: tiny, beginner, cyrptography, math"""
print('Caesar Cipher Hacker, by Al Sweigart [email protected]')
#let the user specify the message to hack:
print('Enter the encrypted Caesar cipher message to hack.')
message = input('> ')
# Every possible symbol that can be encrypted/decrypted:
# (This must match the SYMBOLS used when encrypting the message.)
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for key in range(len(SYMBOLS)): #Loop through every possible key.
translated = ''
# Decrypt each symbol in the message:
for symbol in message:
if symbol in SYMBOLS:
num = SYMBOLS.find(symbol) #Get the number of the symbol.
num = num - key # Decrypt the number.
# Handle the wrap-around if num is less than 0:
if num < 0:
num = num + len(SYMBOLS)
# Add decrypted number's symbol to translated:
translated = translated + SYMBOLS[num]
else:
# Just add the symbol without decrypting:
translated = translated + symbol
# Display the key being tested, along with its decrypted text:
print('Key #{}: {}' .format(key,translated))