@tech4teachers/

Cryptography Caesar Cipher

From NoStarchPress Cracking codes with Python

Files
• main.py
main.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
```
```# DOES NOT WORK WITH import pyperclip as in the book!!!!

# The string to be encrypted/decrypted:
message = 'The dog is brown'
#This is my secret message.
#guv6Jv6Jz!J6rp5r7Jzr66ntrM

# The encryption/decryption key:
key = 13

# Whether the program encrypts or decrypts:
mode = 'encrypt'
# Set to either 'encrypt' or 'decrypt'.

# Every possible symbol that can be encountered and then encrypted:
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.'
# notice that the empty space between zero and ! is a "character" we need to account for

# Store the encrypted/decrypted form of the message:
translated = ''
#logic time....try to mentally map what is happening here ...to find a part of the message, see if it is in the lookup table ....move along the lookup table list and reassign that spot in the message a new value based on the move instructed by the "key"

for symbol in message:

if symbol in SYMBOLS:
symbolIndex = SYMBOLS.find(symbol)
# Perform encryption/decryption:
if mode == 'encrypt':
translatedIndex = symbolIndex + key
elif mode == 'decrypt':
translatedIndex = symbolIndex - key

# What happens if the letter is near the end of or beginning of the posible symbols list...we need to "wrap around" in order to keep counting the position change our key specifies

# Handle wraparound, if needed:
if translatedIndex >= len(SYMBOLS):
translatedIndex = translatedIndex - len(SYMBOLS)
elif translatedIndex < 0:
translatedIndex = translatedIndex + len(SYMBOLS)
translated = translated + SYMBOLS[translatedIndex]
# WATCH YOUR INDENTS ...THEY MATTER!!!!
else:
# some other odd symbol not in the list of possibles is there so just reuse it don't translate it ....Append the symbol without encrypting/decrypting: test with an asterisk*
translated = translated + symbol

# We are done with our logic function and baclk out at zero(0) indent level
# Output the translated string:
print(translated)

# copy the message to the computer's clipboard for copying and pasting ...pyperclip ...even though it loads it just can't do it for you here so do it manually yourself

```