Skip to content

Instantly share code, notes, and snippets.

@benrules2
Last active September 27, 2018 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benrules2/2f16c2ba89fbf5357c1767f757a124d5 to your computer and use it in GitHub Desktop.
Save benrules2/2f16c2ba89fbf5357c1767f757a124d5 to your computer and use it in GitHub Desktop.
Shift Cipher
class ShiftCipher:
def __init__(self, N = 13):
[...]
def encrypt_message(self, message):
encrypted = ""
#lowercase the message as alphabets are lowercase
message = message.lower()
for letter in message:
if self.original_alphabet.count(letter) > 0:
#if the letter exists in original alphabet, find the index and add encrypted form
index = self.original_alphabet.index(letter)
encrypted += self.cipher_alphabet[index]
else:
#if it does not exist in cipher alphabet, do not change the character
encrypted += letter
return encrypted
def decrypt_message(self, message):
decrypted = ""
#lowercase the message as alphabets are lowercase
message = message.lower()
for letter in message:
if self.original_alphabet.count(letter) > 0:
#if the letter exists in original alphabet, find the index and add decrypted form
index = self.cipher_alphabet.index(letter)
decrypted += self.original_alphabet[index]
else:
#if it does not exist in original alphabet, do not change the character
decrypted += letter
return decrypted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment