I am trying in Python Django to create a system for encrypting and decrypting messages. Does anyone know why I get an error every time I try to read a message: “The encrypted message is invalid and cannot be decrypted”. Below is my code:
`class MessageSerializer(serializers.ModelSerializer):
created_by = BasicUserSerializer(read_only=True)
<code>class Meta:
model = Message
fields = '__all__'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
load_dotenv()
self.ENCRYPTION_KEY = "key_generated_by_fernet.generate_key()"
self.fernet = Fernet(self.ENCRYPTION_KEY)
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['text'] = self.decrypt(instance.text)
return representation
def encrypt(self, message):
encoded_message = message.encode()
encrypted_message = self.fernet.encrypt(encoded_message)
return encrypted_message
def decrypt(self, encrypted_message):
try:
decrypted_message = self.fernet.decrypt(encrypted_message)
decoded_message = decrypted_message.decode()
return decoded_message
except InvalidToken:
raise ValueError("The encrypted message is invalid and cannot be decrypted.")`
</code>
<code>class Meta:
model = Message
fields = '__all__'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
load_dotenv()
self.ENCRYPTION_KEY = "key_generated_by_fernet.generate_key()"
self.fernet = Fernet(self.ENCRYPTION_KEY)
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['text'] = self.decrypt(instance.text)
return representation
def encrypt(self, message):
encoded_message = message.encode()
encrypted_message = self.fernet.encrypt(encoded_message)
return encrypted_message
def decrypt(self, encrypted_message):
try:
decrypted_message = self.fernet.decrypt(encrypted_message)
decoded_message = decrypted_message.decode()
return decoded_message
except InvalidToken:
raise ValueError("The encrypted message is invalid and cannot be decrypted.")`
</code>
class Meta:
model = Message
fields = '__all__'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
load_dotenv()
self.ENCRYPTION_KEY = "key_generated_by_fernet.generate_key()"
self.fernet = Fernet(self.ENCRYPTION_KEY)
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['text'] = self.decrypt(instance.text)
return representation
def encrypt(self, message):
encoded_message = message.encode()
encrypted_message = self.fernet.encrypt(encoded_message)
return encrypted_message
def decrypt(self, encrypted_message):
try:
decrypted_message = self.fernet.decrypt(encrypted_message)
decoded_message = decrypted_message.decode()
return decoded_message
except InvalidToken:
raise ValueError("The encrypted message is invalid and cannot be decrypted.")`
Has anyone ever had a similar problem and knows how to solve it?
New contributor
Jakub Jordanek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.