Skip to content

Instantly share code, notes, and snippets.

@benrules2
Created September 27, 2018 02:17
Show Gist options
  • Save benrules2/6914586bd41f65605a9e329097524a10 to your computer and use it in GitHub Desktop.
Save benrules2/6914586bd41f65605a9e329097524a10 to your computer and use it in GitHub Desktop.
Shift Cipher
class ShiftCipher:
def __init__(self, N = 13):
self.original_alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m",
"n","o","p","q","r","s","t","u","v","w","x","y","z"]
self.cipher_alphabet = self.original_alphabet[N:]
self.cipher_alphabet.extend(self.original_alphabet[0:N])
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
import sys
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Not enough arguments, please provide e/d followed by the message")
N = 13
cipher = ShiftCipher(N)
if sys.argv[1] == "e":
message = cipher.encrypt_message(sys.argv[2])
print("Encrypted message: \n {}".format(message))
elif sys.argv[1] == "d":
message = cipher.decrypt_message(sys.argv[2])
print("Decrypted message: \n {}".format(message))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment