Encryptor & Decryptor - Python Programming Exercise

In this exercise, you will develop a Python class called "Encryptor" for text encryption and decryption. This exercise is perfect for practicing class definition, method implementation, and string manipulation in Python. By implementing this class, you will gain hands-on experience in handling class definitions, method implementation, and string manipulation in Python. This exercise not only reinforces your understanding of object-oriented programming but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Python Classes in OOP

 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

 Copy 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

 More Python Programming Exercises of Mastering Python Classes in OOP

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Advanced Number Systems

    In this exercise, you will develop a Python program to represent complex numbers, which consist of a real part and an imaginary part. This exercise is perfect ...

  •  Table, Coffee Table, and Legs

    In this exercise, you will develop a Python project based on the tables and coffee tables example, but now introduce a new class named "Leg". This exercise is ...

  •  Catalog

    In this exercise, you will develop a Python class diagram for a catalog utility that stores details about music files, movies, and computer programs. This exercise...

  •  Random Value Generation

    In this exercise, you will develop a Python project with a class RandomNumber that includes three static methods. This exercise is perfect for practicing class...

  •  Converting Text to HTML

    In this exercise, you will develop and program a Python class "TextToHTML" that can convert multiple texts entered by the user into a sequence of HTML lines. This ...

  •  Text on Screen Class

    In this exercise, you will develop a Python class called "DisplayText" that allows you to show text at specific coordinates on the screen. This exercise is per...