Exercise
Encryptor & Decryptor
Objective
Develop a python class "Encryptor" for text encryption and decryption.
It will include an "Encrypt" method, which takes a string and returns an encrypted version. This method will be static, so no instance of the "Encryptor" class is needed.
There will also be a "Decrypt" method.
For this first version, the encryption method will be quite simple: to encrypt, we will increment each character by 1. For instance, "Hello" would turn into "Ifmmp", and to decrypt, we would decrement each character by 1.
An example of usage could be:
newText = Encryptor.Encrypt("Hello")
Example Python Exercise
Show Python Code
class Encryptor:
@staticmethod
def Encrypt(text):
"""
Static method to encrypt the text.
To encrypt, each character is incremented by 1.
"""
encrypted_text = ''.join(chr(ord(char) + 1) for char in text)
return encrypted_text
@staticmethod
def Decrypt(text):
"""
Static method to decrypt the text.
To decrypt, each character is decremented by 1.
"""
decrypted_text = ''.join(chr(ord(char) - 1) for char in text)
return decrypted_text
# Example usage
if __name__ == "__main__":
original_text = "Hello"
encrypted_text = Encryptor.Encrypt(original_text)
decrypted_text = Encryptor.Decrypt(encrypted_text)
print(f"Original Text: {original_text}")
print(f"Encrypted Text: {encrypted_text}")
print(f"Decrypted Text: {decrypted_text}")
Output
Original Text: Hello
Encrypted Text: Ifmmp
Decrypted Text: Hello
Share this Python Exercise