Skip to content

Instantly share code, notes, and snippets.

@PHenegan
Last active April 12, 2021 18:02
Show Gist options
  • Save PHenegan/bdf76ffbe332c60ad34e5ad49847de16 to your computer and use it in GitHub Desktop.
Save PHenegan/bdf76ffbe332c60ad34e5ad49847de16 to your computer and use it in GitHub Desktop.
Mobile CSP Cipher App
import math
def main():
optionsList = ["Encrypt a message", "Encrypt a message with modified spacing", "Decrypt a message", "Decrypt a message and remove spacing", "Quit"]
correctInput = False
userChoice = 0
userString = ""
userOffset = 0
newMessage = ""
print("====Welcome to [CipherApp]====")
#prints options and gets user's choice until user enters correctly
while (correctInput != True):
print("\nOptions:")
for i in range(len(optionsList)):
print("[" + str(i+1) + "]", optionsList[i])
userChoice = eval(input("Enter the number for one of the above options: "))
if (userChoice < 1 or userChoice > len(optionsList)):
print("Invalid input: please enter the number for one of the options above.")
else:
correctInput = True
#EXITS THE PROGRAM (caps to make it stand out for debugging purposes)
if (userChoice == 5):
exit()
userString = input("Enter the message: ")
#ensures that the offset will always be a positive whole number.
userOffset = int(math.fabs(eval(input("Enter the cipher offset: "))))
#why doesn't python have switch statements :(
if (userChoice == 1):
newMessage = encrypt(userString, userOffset, False)
elif (userChoice == 2):
newMessage = encrypt(userString, userOffset, True)
elif (userChoice == 3):
newMessage = decrypt(userString, userOffset, False)
elif (userChoice == 4):
newMessage = decrypt(userString, userOffset, True)
print ("The new message is:", newMessage)
#encrypts the message with a caesar cipher with optional custom space formatting
def encrypt(message, offset, useCustomFormatting):
plainSymbols = ["abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "0123456789", "`~!@#$%^&*()_+-={}[]:;\"\'<>,.?/|\\", " "]
cipherSymbols = []
encMessage = ""
#generates the cipher symbol sets based on the offset provided
for i in range(len(plainSymbols)):
cipherSymbols.append("")
for j in range(len(plainSymbols[i])):
cipherSymbols[i] += plainSymbols[i][(j + offset) % len(plainSymbols[i])]
#If useCustomFormatting is true, removes spaces so they can be reform
if (useCustomFormatting):
message = "".join(message.split())
for i in range(len(message)):
foundSymbol = False
for j in range(len(cipherSymbols)):
if (foundSymbol == False and plainSymbols[j].find(message[i]) != -1):
#for the sake of readability I separated this into two lines
index = plainSymbols[j].find(message[i])
encMessage += cipherSymbols[j][index]
#since any character will only fall into one group, then the loop will terminated after one is successfully found
break
if (useCustomFormatting and (i + 1) % 4 == 0):
encMessage += ' '
return encMessage
#decrypts a caesar cipher message with optional removal of spaces
def decrypt(message, offset, useCustomFormatting):
decMessage = ""
decMessage = encrypt(message, -offset, False)
if (useCustomFormatting):
decMessage = "".join(decMessage.split())
return decMessage
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment