1

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, Encrypting using public key and decrypting using private key.
would you give snippet on how to store data in postgresql...regards
I would store as bytea. Note that bytea may need to be unescaped on retrieval.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.