Tara Singh
Tara Singh

Reputation: 1841

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 decrypt 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

Upvotes: 1

Views: 4883

Answers (2)

Soferio
Soferio

Reputation: 463

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

Upvotes: 1

Jochen Ritzel
Jochen Ritzel

Reputation: 107628

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).

Upvotes: 1

Related Questions