Python, PostgresSQL and byte arrays

Status
Not open for further replies.

Leaf

Ars Scholae Palatinae
939
Subscriptor
I believe I'm having some trouble with data type conversions. Currently I have an encrypted string stored in a database where the database column is of type bytea. When I fetch the record, Python interprets the resulting data as:

Code:
(<read-only buffer for 0xXXXXXXXX, size 256, offset 0 at 0xXXXXXXXX>,)

Now, I'm pretty new to Python, but after some Googling it appears that I need to use the psycopg2 module's Binary function to convert it into something that Python can display. Converting it first to Binary gives me:
Code:
<psycopg2._psycopg.Binary object at 0xXXXXXXXX>

And then after converting that to a binary string, I get what appears to be an octal representation of the data:

Code:
\\315k\\261\\252\\023\\243\\215x\\369\\346\\376"^\\262f\\176\\000\\000\\000\\000\\000\\000 ...

What's bothering me here is the extra characters (i.e. the k, x, "^, f, etc.) My first guess is that things aren't being escaped correctly in the data type conversion?

Basically all I'm trying to do is get the encrypted field, convert it into an encrypted string, and then send that string to a decrypt method later on.

Any help or pushes in the right direction with this would be appreciated. Thanks.
 

Alamout

Ars Legatus Legionis
27,285
I think LH317 is right, it's just the way that Python is displaying the data.

Basically, you stored some binary data and you're trying to look at it like it's text. That's just not gonna make sense, so don't worry about what it looks like.

Alternatively, maybe you didn't store binary. Or you stored something else, like a base64 representation. Once you figure out exactly how the stuff got in there, you'll be able to reverse the process. Python should have all the libraries you need to do so, but you have to figure out what was done.
 

Leaf

Ars Scholae Palatinae
939
Subscriptor
Thanks for the replies.

It does indeed look like the the data there. I know the data is sent in utf-8 encoding, and after messing around a bit with a small test module, I was able to recreate the octal formatted output by converting the encrypted string to unicode and then using psycopg2's Binary method. The conversion from hex to octal does seem to be there and there are also some characters that are present as in the example I posted before that aren't octal (i.e. X and ; ), so I thnk LordHunter317 is correct in that they are simply being displayed that way but are not indicators of data corruption.

So rather than calling the Binary method on the retreived encrypted data, I think I can assume I have a utf-8 encoded byte array, and try and work with that.

I've been using the ctypes module in Python to interact with a .DLL file that contains the encrypt/decrypt methods, so hopefully there's a way to get the string data back to a format i can use.

Out of curiosity, when using the repr() function on an encrypted string, I get most of the special characters to display as hex values. If the string itself contains the character representation of the repr() output, is there a way to ... unrepr()?

So, for example, instead of:
Code:
%\xc4\xff\xb1\x08  i can get  %Äÿ±
 

Leaf

Ars Scholae Palatinae
939
Subscriptor
The reason why I'd bother is so I can send the string to the decrypt method. If the string to be encrypted started out with ASCII encoding, then went to UTF-8, I need a way to convert the unicode back to ASCII, right? The problem is that the string itself (not just the repr() of it) contains the unicode representation that you'd expect from a repr() return. For example:

Code:
print teststring
';\x10\x1c\xed,%\xc4\xff\xb1\x08Xb8\x1b\xa8\xfb'

print repr(teststring)
u"';\\x10\\x1c\\xed,%\\xc4\\xff\\xb1\\x08Xb8\\x1b\\xa8\\xfb'"

Sorry if I'm being a newb with this, but this Python task got thrown at me and I'm learning all of this stuff somewhat on the fly. I appreciate the responses!
 

Alamout

Ars Legatus Legionis
27,285
Ah. Are you running Python from a command-line, or as a GUI?

When I print that string using IDLE (the Python GUI, comes with a standard installation on Windows), I get the Unicode characters. If the interface you're using can't display those characters, it has to display the hex codes instead.

Actually, I suppose that while 'print' works fine in IDLE, it doesn't do much for you in an actual script, since the output is written to a console (that might not support UTF-8).. hm. You could print them to a file, and then look at the file using something that can interpret those characters, I suppose.

edit: actually, I can print the Unicode characters when I run Python from the command line. Maybe it's a matter of not having full Unicode support in your Python installation?
 

Leaf

Ars Scholae Palatinae
939
Subscriptor
Well, sort of both command line and GUI. I'm usin PyScripter as the IDE for this (it's awful), so I'm viewing the Python interpreter output in a nested window.

Right now I'm just really flustered with this, and I may very well be going down the wrong path in terms of what I'm supposed to be doing.

Basically what I need to do is:
1.) Retrieve the encrypted data (of type bytea) from the database.
2.) Store it into something that Python knows how to interpret.
3.) Convert it into a ctypes module's string type and pass it off to another method.

Again, I appologize if I'm being slow on the uptake here. I've so far managed to get Python to interact with the DLL file that I created and use its methods, I thought that would be the hard part and did not expect it to be a struggle just to see what the heck was retreived from the database.

I'm currently attempting to use the bytearray type, but it complains that it doesn't know what the database is returning it and wants either a string or an int. Typecasting the data to a string just gives me a byte array containing "(<read-only buffer for 0xXXXXXXXX, size 256, offset 0 at 0xXXXXXXXX>,)", which doesn't help me out.

I'm also considering looking at the curses library, BLOBS, etc. Really at this point I'm pretty lost as to what direction to go. Had no idea that the BYTEA datatype would be such a pain to work with.
 
Status
Not open for further replies.