Skip to content

Instantly share code, notes, and snippets.

@benrules2
Created October 20, 2018 00:56
Show Gist options
  • Save benrules2/6c534fba23f5c6d99f19fdd6ff42d6d1 to your computer and use it in GitHub Desktop.
Save benrules2/6c534fba23f5c6d99f19fdd6ff42d6d1 to your computer and use it in GitHub Desktop.
import sys
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"]
class ShiftCipher:
def __init__(self, N = 13):
self.cipher_alphabet = alphabet[N:]
self.cipher_alphabet.extend(alphabet[0:N])
def encrypt(self, message):
encrypted = ""
message = message.lower()
for letter in message:
if alphabet.count(letter) > 0:
letter_index = alphabet.index(letter)
encrypted += self.cipher_alphabet[letter_index]
else:
encrypted += letter
return encrypted
def decrypt(self, message):
decrypted = ""
message = message.lower()
for letter in message:
if alphabet.count(letter) > 0:
letter_index = self.cipher_alphabet.index(letter)
decrypted += alphabet[letter_index]
else:
decrypted += letter
return decrypted
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Please provide action (e/d) and message as CLI options")
action = sys.argv[1]
phrase = sys.argv[2]
rotation_distance = 3
cipher = ShiftCipher(rotation_distance)
if action == "e":
phrase = cipher.encrypt(phrase)
print("Encrypting: {}".format(phrase))
elif action == "d":
phrase = cipher.decrypt(phrase)
print("Decrypting: {}".format(phrase))
else:
print("Invalid action, please provide e or d for encryption and decryption respectively")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment