I'm having trouble encrypting bytes from a PyObject using XOR.
For now I only managed to print the bytes as an encoded string (with PyUnicode_AsEncodedString
):
Here's what I tried (taken from this SO answer)
PyObject* repr = PyObject_Repr(wf.str); // wf.str is a PyObject *
PyObject* str = PyUnicode_AsEncodedString(repr, "utf-8", "~E~");
const char *bytes = PyBytes_AS_STRING(str);
printf("REPR: %s\n", bytes);
Py_XDECREF(repr);
Py_XDECREF(str);
From here on, I don't know what to do anymore.
I also tried to access bytes only using PyBytes_AS_STRING(wf.str)
and then proceed with the encryption, but it only returned one byte.
There is a way to XOR encrypt bytes taken from a PyObject? Something like this:
bytes = getBytesFromPyObject(wf.str)
encrypted = XOREncryption(bytes)
AssignBytesToPyObject(encrypted, wf.str)
Note: I don't know much about C, all of this is almost new to me.
Edit: I'm using C instead of Python because I need to implement a function that uses XOR encryption in a built-in module for Python3.