-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.py
More file actions
65 lines (52 loc) · 2.33 KB
/
Code.py
File metadata and controls
65 lines (52 loc) · 2.33 KB
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
55
56
57
58
59
60
61
62
63
64
65
#This program is the starter code for the Cipher Usability Project.
# This code is inspired by Cracking Codes with Python: An Introduction to Building and Breaking Ciphers by Al Sweigart https://www.nostarch.com/crackingcodes (BSD Licensed)
import string
# Global variables
runProgram = "yes"
initialPosition = 0
shiftedPosition = 0
shiftedMessage = ""
lettersLower = string.ascii_lowercase
lettersUpper = string.ascii_uppercase
numbers = string.digits
symbols = string.punctuation
possibleCharacters = lettersLower + lettersUpper + numbers + symbols
# Define the function called encryptOrDecrypt
def encryptOrDecrypt():
global shiftedPosition
if mode.lower() == "encrypt":
shiftedPosition = initialPosition + key
elif mode.lower() == "decrypt":
shiftedPosition = initialPosition - key
# Define the function called wraparound
def wraparound():
global shiftedPosition
if shiftedPosition >= len(possibleCharacters):
shiftedPosition = shiftedPosition - len(possibleCharacters)
elif shiftedPosition < 0:
shiftedPosition = shiftedPosition + len(possibleCharacters)
# Run code
# Introduction
print("Welcome! This program will encrypt or decrypt your secret message using the Caesar cipher. \n\nWhen creating your message, you may only choose from the following characters: " + possibleCharacters + "\n\nLet's get started!\n")
while runProgram.lower() == "yes":
# Receive user input
initialMessage = input("What is your message? ")
key = int(input("\nWhat is the key? Choose a number from 0 to 93. "))
mode = input("\nDo you want to encrypt or decrypt? ")
# Encrypt or decrypt the message
for character in initialMessage:
if character in possibleCharacters:
initialPosition = possibleCharacters.find(character)
encryptOrDecrypt()
wraparound()
shiftedMessage = shiftedMessage + possibleCharacters[shiftedPosition]
else:
shiftedMessage = shiftedMessage + character
# Print the shifted message
if mode.lower() == "encrypt" or mode.lower() == "decrypt":
print("\nYour " + mode.lower() + "ed message is: " + shiftedMessage)
runProgram = input("\nDo you want to encrypt or decrypt another message? Type yes or no. ")
else:
print("\nOops! :( The program reads an error. Try again and make sure you type either 'encrypt' or 'decrypt.'")
print ("\n")
shiftedMessage = ""