"""
Coding challenge from SoloLearn
Created: 09/25/2019 22:48 UTC-8

Title: Caesar Cipher

Description:
    The Caesar cipher is a type of substitution cipher in which each letter
    in the plaintext is shifted to a certain number of places down the alphabet.
    For example, with a shift of 1, A would be replaced by B, B would become C, and so on.
    The method is named after Julius Caesar, who allegedly used it to communicate with his generals.

Example:
    Input: 
        "defend the east wall of the castle"
    Output:
        "efgfoe uif fbtu xbmm pg uif dbtumf"

"""

class caesarCipher(object):

    def __init__(self, message):
        self.message = message
        self.message_string = message

    def encipher(self, key):
        key %= 26
        encrypted_string = ''
        for x in self.message:
            unic = ord(x)
            for indx in range(0, key):
                if unic == 32:
                    break
                elif unic == 90 or unic == 122:
                    unic -= 25
                else:
                    unic += 1
            encrypted_string += chr(unic)
        print("Message Enciphered.")
        self.message = encrypted_string
        return encrypted_string

    def decipher(self, key):
        key %= 26
        encrypted_string = ''
        for x in self.message:
            unic = ord(x)
            for indx in range(0, key):
                if unic == 32:
                    break
                elif unic == 65 or unic == 97:
                    unic += 25
                else:
                    unic -= 1
            encrypted_string += chr(unic)
        print("Message Deciphered.")
        self.message = encrypted_string
        return encrypted_string


str_in = input("Input Text: ")
message = caesarCipher(str_in)
key = int(input("enter key(must be int > 0): "))
print(message.message_string)
print(message.encipher(key))
print(message.message_string)
print(message.decipher(key))