Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lukasrichters14/668405a08552dd5bb56bcccf295849d9 to your computer and use it in GitHub Desktop.
Save lukasrichters14/668405a08552dd5bb56bcccf295849d9 to your computer and use it in GitHub Desktop.
An encryption/decryption program that uses the Caesar and Affine encryption methods to encrypt a string of text.
###########################################################
# Computer Project #3
#
# Algorithm
# Prompt to encrypt, decrypt, or quit
# Initialize variables that need to be appended to or used in the program
# Generate string of all lowercase alphabetic characters
# Ask for keyword of only alphabetic charactes and less than 26 characters
# Ask for a message to encrypt/decrypt
# If Encrypt:
# Create Caesar cipher:
# Append each letter in the keyword (if not already there)
# Append each letter of the alphabet (if not already there)
# Create the Affine cipher:
# Loop through the Caesar key
# For each index perform the Affine operation ((5x + 8) % 26)
# Append the character in the Caesar key index from Affine operation
# Encrypt with Caesar cipher:
# Just add all non-alphabetic characters
# If it is a letter, find its index in the alphabet and replace it
# with the letter in that index in the Caesar cipher key
# Encrypt with Affine cipher:
# Just add all non-alphabetic characters
# If it is a letter, find its index in the Caesar key and replace it
# with the letter in that index in the Affine cipher key
# Display encrypted message
# If Decrypt:
# Create Caesar cipher:
# Append each letter in the keyword (if not already there)
# Append each letter of the alphabet (if not already there)
# Create the Affine cipher:
# Loop through the Caesar key
# For each index perform the Affine operation ((5x + 8) % 26)
# Append the character in the Caesar key index from Affine operation
# Decrypt with Affine cipher:
# Just add all non-alphabetic characters
# If it is a letter, find its index in the Affine cipher key and
# replace it with the letter in that index in the Caesar cipher key
# Decrypt with Caesar cipher:
# Just add all non-alphabetic characters
# If it is a letter, find its index in the Caesar key and replace it
# with the letter in that index in the alphabet string
# Display decrypted message
# Display closing message
###########################################################
import string
# Ask if user wants to quit or encrypt/decrypt
answer = input("Would you like to (D)ecrypt, (E)ncrypt or (Q)uit? ").upper()
while answer != 'Q':
# Initialize variables that need to be appended to or used in the program
ALPHABET_STR = string.ascii_lowercase
c_key = ""
c_encrypted_text = ""
a_key = ""
a_encrypted_text = ""
A = 5
B = 8
M = 26
a_decrypted_text = ""
c_decrypted_text = ""
# Encrypt
if answer == 'E':
keyword = input("Please enter a keyword: ").lower()
# Make sure the keyword is all letters and not longer than 26
# characters.
while keyword.isalpha() == False or len(keyword) > 26:
print("There is an error in the keyword. It must be all letters\
and a maximum length of 26")
keyword = input("Please enter a keyword: ").lower()
message = input("Enter your message: ").lower()
# Create the Ceasar cipher key.
# With the keyword first.
for index, letter in enumerate(keyword):
if letter not in c_key:
c_key += letter
# Then the rest of the letters of the alphabet.
for index, letter in enumerate(ALPHABET_STR):
if letter not in c_key:
c_key += letter
# Create the Affine cipher key.
# Loop through each letter in the Caesar key, performing the Affine
# encryption operation, and then append the letter value to the Affine
# key.
for index, letter in enumerate(c_key):
affine_index = (A * index + B) % M
a_key += c_key[affine_index]
# Encrypt using the Caesar cipher.
for letter in message:
# If the string in this index is not a letter, just add it to the
# ecrypted message.
if letter.isalpha() == False:
c_encrypted_text += letter
# If the string in this index is a letter, find the index where
# that letter normally appears in the alphabet. Then, substitute
# that letter for whatever letter is in that index in the Caeasr
# cipher.
else:
index_in_alphabet = ALPHABET_STR.find(letter)
c_encrypted_text += c_key[index_in_alphabet]
# Encrypt using the Affine cipher.
for letter in c_encrypted_text:
# If the string in this index is not a letter, just add it to the
# ecrypted message.
if letter.isalpha() == False:
a_encrypted_text += letter
# If the string in this index is a letter, find the index where
# that letter appears in the Caesar cipher. Then, substitute that
# letter for whatever letter is in that index in the Affine cipher.
else:
index_in_c_key = c_key.find(letter)
a_encrypted_text += a_key[index_in_c_key]
print("your encoded message: ", a_encrypted_text)
# Decrypt
if answer == 'D':
keyword = input("Please enter a keyword: ").lower()
# Make sure the keyword is all letters and not longer than 26
# characters.
while keyword.isalpha() == False or len(keyword) > 26:
print("There is an error in the keyword. It must be all letters\
and a maximum length of 26")
keyword = input("Please enter a keyword: ").lower()
message = input("Enter your message: ").lower()
# Create the Ceasar cipher key.
# With the keyword first.
for index, letter in enumerate(keyword):
if letter not in c_key:
c_key += letter
# Then the rest of the letters of the alphabet.
for index, letter in enumerate(ALPHABET_STR):
if letter not in c_key:
c_key += letter
# Create the Affine cipher key.
# Loop through each letter in the Caesar key, performing the Affine
# encryption operation, and then append the letter value to the Affine
# key.
for index, letter in enumerate(c_key):
affine_index = (A * index + B) % M
a_key += c_key[affine_index]
# Decrypt text using Affine cipher
for index, letter in enumerate(message):
# If the string in this index is not a letter, just add it to the
# decrypted message.
if letter.isalpha() == False:
a_decrypted_text += letter
# If the string in this index is a letter, find the index where
# that letter appears in the Affine cipher key. Then, substitute
# that letter for whatever letter in is that index in the Caeasr
# cipher.
else:
affine_index = a_key.find(letter)
a_decrypted_text += c_key[affine_index]
# Decrypt using Caesar cipher
for index, letter in enumerate(a_decrypted_text):
# If the string in this index is not a letter, just add it to the
# ecrypted message.
if letter.isalpha() == False:
c_decrypted_text += letter
# If the string in this index is a letter, find the index where
# that letter appears in the Caesar cipher key. Then, substitute
# that letter for whatever letter in is that index in the alphabet
# string.
else:
caesar_index = c_key.find(letter)
c_decrypted_text += ALPHABET_STR[caesar_index]
print("your decoded message: ", c_decrypted_text)
# Ask if user wants to quit or encrypt/decrypt
answer = input("Would you like to (D)ecrypt, (E)ncrypt or (Q)uit? ").upper()
print("See you again soon!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment