Member-only story
Encryption and Decryption in Python Using OOP
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 withoriginal_data
to store the user's input andencrypted_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 theencrypted_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.