Skip to content

Instantly share code, notes, and snippets.

@urwrstkn8mare
Last active July 18, 2020 12:04
Show Gist options
  • Save urwrstkn8mare/f147407dba2528d635eedcb0cd1c95be to your computer and use it in GitHub Desktop.
Save urwrstkn8mare/f147407dba2528d635eedcb0cd1c95be to your computer and use it in GitHub Desktop.
Caesar cipher (Comp. Sci. 2020)
# Title: Caesar Cipher Encryption and Decryption Python Functions
# Author: Samit Shaikh
# Purpose: To underestand an encryption algorithm and implement it in Python.
# Last Revision: 17/06/2020
# Note: In this revision I removed alot of the type checking because I have
# learned that in Python that its good practice not to unlike most other
# languages as it assumes the developer knows what they are doing with
# the function. I also added comments to describe it.
ALPHABET = "abcdefghijklmnopqrstuvwxyz" # The alphabet in lowercase.
# ==================== CAESAR ENCRYPTION ALGORITHM ===========================
def caesar_e(
plainText: str,
keyvalue: int,
*,
alphabet=ALPHABET,
includeForeignChars=True,
caseStrat="strict"
):
# Create an empty string for the encrypted string.
encrypted = ""
# Loop throught the text input.
for char in plainText:
# Create a copy of newChar to modify.
newChar = char
# If the case strategy is to maintain or ignore and and foreign
# characters are not to be included.
if caseStrat == "maintain" or "ignore" and not includeForeignChars:
# Make newChar lowercase.
newChar = char.lower()
if newChar in alphabet:
# If the lowercased char is in the alphabet...
toAppend = alphabet[
# Join together the characters in the array of chars incase
# it isn't a string already. Add the key value to the the
# ASCII value of the char.
("".join(alphabet).find(newChar) + keyvalue)
% len(alphabet)
]
# Add the new character to the encrypted string.
encrypted += (
(toAppend.upper() if char.isupper() else toAppend)
if caseStrat == "maintain"
else toAppend
)
else:
# Just add the new char to the encrypted string if new char
# is not not in the alphabet and include foreign chars is
# enabled.
if includeForeignChars:
encrypted += newChar
# Return the final encrypted string.
return encrypted
# ==================== CAESAR DECRYPTION ALGORITHM ===========================
def caesar_d(
ciphertext: str,
keyvalue: int,
*,
alphabet=ALPHABET,
includeForeignChars=True,
caseStrat="strict"
):
# Just run the encryption algorithm with a negative key value to
# make it decrypt instead.
return caesar_e(
ciphertext,
0 - keyvalue,
alphabet=alphabet,
includeForeignChars=includeForeignChars,
caseStrat=caseStrat,
)
# ============================ FOR TESTING ===================================
if __name__ == "__main__":
string = str(input("To encrypt: "))
key = int(input("Key value: "))
caseStrategy = str(input("Case strategy (maintain/ignore/strict): "))
includeForeignChars = str(input("Include foreign characters? (t/f): "))
includeForeignChars = True if includeForeignChars == "t" else False
caesar = None
while caesar is None:
encryptQ = str(input("Encrypt or decrypt (e/d)"))
if encryptQ == "e":
caesar = caesar_e
elif encryptQ == "d":
caesar = caesar_d
else:
print("Incorrect input, try again...")
print(
caesar(
string,
key,
includeForeignChars=includeForeignChars,
caseStrat=caseStrategy,
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment