Member-only story

Encryption and Decryption in Python Using OOP

Python Coding
1 min readSep 4, 2024

--

class Encrypt:
def __init__(self):
self.send = ""
self.res = []

# Sender encrypts the data
def sender(self):
self.send = input("Enter the data: ")
self.res = [ord(i) + 2 for i in self.send]
print("Encrypted data:", "".join(chr(i) for i in self.res))

class Decrypt(Encrypt):
# Receiver decrypts the data
def receiver(self):
decrypted_data = "".join(chr(i - 2) for i in self.res)
print("Decrypted data:", decrypted_data)

# Usage
obj = Decrypt()
obj.sender()
obj.receiver()

#source code --> clcoding.com
Encrypted data: jvvru<11z0eqo1eneqfkpi
Decrypted data: https://x.com/clcoding

Explanation:

Class Encrypt:

  • __init__: Initializes with original_data to store the user's input and encrypted_data to store the encrypted result.
  • encrypt: Takes user input, encrypts it by adding 2 to each character's ASCII value, and stores the encrypted values.

Class Decrypt:

  • Inherits from Encrypt.
  • decrypt: Decrypts the encrypted_data by subtracting 2 from each ASCII value, recovering the original text.

Workflow:

  • The Encrypt class encrypts the input text.
  • The Decrypt class decrypts the encrypted text to recover the original data.

--

--

Python Coding
Python Coding

Written by Python Coding

Learn python tips and tricks with code I Share your knowledge with us to help society. Python Quiz: https://www.clcoding.com/p/quiz-questions.html

Responses (1)