Inserting Encrypted Data in Postgres via SQLALchemy
I want to encrypt a string using RSA algorithm and then store that string into postgres database using SQLAlchemy in python. Then Retrieve the encrypted string and decr开发者_JS百科ypt it using the same key. My problem is that the value gets stored in the database is not same as the actual encrypted string. The datatype of column which is storing the encrypted value is bytea. I am using pycrypto library. Do I need to change the data in a particular format before inserting it to database table?
Any suggestions please.
Thanks, Tara Singh
By "same key" you mean "the other key", right? RSA gives you a keypair, if you encrypt with one you decrypt with the other ...
Other than that, it sounds like a encoding problem. Try storing the data as binary or encode the string with your databases collation.
Basically encryption gives you bytes but you store them as a string (encoded bytes).
I think the SQLAlchemy documents contain a 'recipe' example which uses the 'hybrid' property. I do not profess to understand it fully or tried it yet, but you may find it useful:
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/SymmetricEncryption
精彩评论