0

I tried to encrypt a text in python using pyaes package:

import os
import pyaes
aes = pyaes.AESModeOfOperationCTR(os.urandom(16))
result = aes.encrypt("test")

but I'm getting a error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte

and also I tried aes.encrypt("test".encode())

What I'm doing wrong here?

jps
  • 20,041
  • 15
  • 75
  • 79
  • I had no issues whatsoever running your code snippet on both Python 3.9 and Python 2.7.18. What is the Python version you are using ? Could you give a more detailed traceback ? – Icebreaker454 Mar 19 '21 at 10:32
  • hi, my python version is 3.9.2 and this all the traceback `File "C:\Users\DASUSER1\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "C:\Users\DASUSER1\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 257, in iterencode return _iterencode(o, 0) File "C:\wdir\dms_django_react\venv\lib\site-packages\rest_framework\utils\encoders.py", line 50, in default return obj.decode()` – Arun nandha Mar 19 '21 at 10:50

1 Answers1

1

You are possibly trying to serialize your result to json.

The encryption result is bytes, and these bytes could not be directly decoded to a string.

Use base64 instead, as in this question

result = aes.encrypt("test")
to_dump = base64.b64encode(result).decode()

import json
json.dumps(to_dump)  # this would output a b64encoded version of your secret, which could later be decoded and decrypted on the client.
Icebreaker454
  • 1,031
  • 7
  • 12